Skip to content Skip to sidebar Skip to footer

Go Back To Activity Without Recreating (without Invoking Oncreate())

I would like to ask similar question to: Go back to previous screen without creating new instance However I don't want Activity A go through onCreate callback, I would like to retu

Solution 1:

There's no need to finish() activities as you navigate through your application. Instead, you can maintain your Activity back-stack and still achieve your goal. Let's say you have 4 activites like so:

A --> B --> C -->D

where D is the topmost activity and A is root activity. If you are 'falling back' to activity B, then you'll need to do two things in order to avoid hitting B's onCreate method.

1.) Make B a "SingleTask" activity. You can do this in the Android Manifest. To be brief, this means that only one 'instance' of B will ever exist within this Task. If B is already running when it's called then it will simply be brought to the front. This is how you do that.

<activityandroid:name=".ui.MyActivity"android:launchMode="singleTask"/>

But you don't want to just bring B to the front. You want to 'fall back' to B so that your stack looks like

A--> B

2.) Add this flag to your intent that 'starts' B. This ensures that C and D are removed.

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Now when 'D' calls new Intent to B, B will be resumed and C and D will be removed. B will not be recreated, it will only call onNewIntent.

Solution 2:

When navigating from A to B do not finish Activity A. When navigating from B to C finish Activity B. When navigating from C to D finish Activity C.

So from wherever you navigate back finish current Activity and Activity A will get resume again not get created.

Post a Comment for "Go Back To Activity Without Recreating (without Invoking Oncreate())"