androidandroid-activityandroid-lifecycleactivity-state

Can I reuse an Activity in android?


I apologize in advance for my English, I'm developing my first app and I don't know how to proceed. I have an activity that register a thing, I want to provide the option to modify that registration. I thought about reusing the register activity for doing that, I mean, when you click "modify" it opens the register activity with all its fields filled out and you can modify whatever you want (not with empty fields as a new register activity does), and then make a different query to update the record in the SQLite, etc. I don't know how to capture that I'm calling that activity for doing another thing instead of registration. Is that possible? or should I create a new activity for doing that?

Thank you!!


Solution

  • Yes, you can reuse it for example:

    Intent i = new Intent(RegistrationActivity.this, RegistrationActivity.class);
    i.putExtra("id", someId);
    startActivity(i);
    finish();
    

    Remember to call finish() to remove the activity from backstack so when you press back you don't return to previous instance of the same activity

    Then use this to get the id for query:

    String id = getIntent().getStringExtra("id");