batuman
batuman

Reputation: 7304

Switching Activities in Android

I have four activities Activity1, Activity2, Activity3, and Activity4.

Activity1 calls Activity2.

Activity2 calls Activity3, then Activity2 is finished.

Activity3 calls Activity4, Activity3 is finished.

So now I have only Activity1 and Activity4.

Currently the user can see Activity4. Under certain condition, I like to go back to Activity1 but I still want to keep the existing status of Activity4 so that when I come back to Activity4, I still have all like before I switched to Activity1. My questions are

  1. How can I switch back from Activity4 to Activity1 without losing anything from Activity4? So that the user can work with Activity1.
  2. When the user wants to load Activity4, he can go back to the last status of Activity4 so that all the process in Activity4 do not need to be processed again.
  3. When Activity4 is switched back to Activity1, Activity4 will be onStop() state as it is hidden by Activity1. The OS can kill any time if the resources are in shortage. How can I detect Activity4 has been killed?

Activity4 is WebView and it has url links before switching back to Activity1. Thanks

Upvotes: 0

Views: 74

Answers (1)

nKn
nKn

Reputation: 13761

You can achieve that by simply calling:

final Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);              

As you see, finish() is not called, this is because your first Activity will be started and the current will be put in background. The same goes on the first one if you want to recover the forth state without losing it.

Upvotes: 5

Related Questions