I had a fairly simple question regarding activites in an Android application. I’m still a newbie.
In my current android application, I have about 10 activities. If one of the activites is already far back in the activity stack, and I want to reopen it, is it okay to call:
Intent iLogin = new Intent(this, Login.class);
startActivity(iLogin);
If it’s still a little confusing, I’ll give an example. The application launches with activity 1. The user then opens activity 2. Later he opens activity 3 to perform an action. After performing the action, I want the user to go back to activity 1. Is it okay to call the code above? Does it get the activity from th stack, or create a new activity? If no, how can I get the activity from the stack?
UPDATE: I’m interested in recycling activities, rather than creating new ones every time. For instance, not all the activities need to be created new one. Some of them only display data, which can be refreshed upon user request.
Ans. If u use this code, another instance of ur 1st activity is created on the stack , And no it is not OK..
ok. here is an answer based on ur example above:
I suggest u to use
startActivityForResult()to call Activity 2 from Activity 1here , Activity 2 will behave as sub-Activity…u can setResult and call
finish()in 2nd Activity when it succesfully completes its task…but before
finish()put a boolean extra like this:this will indicate that the user has gone through this 2nd activity
Now, u should override,
onActivityResult()in your 1st activity,here u can check the request code and retrieve boolean extra from 2nd activity..
now, the important part:
depending whether the boolean is true or not, u can decide to call 3rd activity using
startActivityForResult()with a different requestcode,Also, in ur
onActivityResult(), u can again check requestcode and do what u wish when activity 3 endsso now the control will be back to ur 1st activity when 3rd activity ends succesfully
Here is an example to get u started.