Roger Garrett
Roger Garrett

Reputation: 349

Android : Returning to prior Activity

When you're in an an Activity (we'll call it A), and you invoke a subsequent Activity (B), perhaps as a result of clicking a button in A, and then RETURN to that prior Activity A, either by clicking the Back button or explicitly calling finish() from within B, it causes A to be completely rebuilt, calling its constructor and its OnCreate() method, etc.

Is there any way to prevent that from happening, so that it actually does return to the prior, already existing, Activity A?

Upvotes: 0

Views: 95

Answers (4)

fernandospr
fernandospr

Reputation: 3061

Calling finish() on ActivityB or pressing back will just destroy ActivityB. ActivityA will not be completely rebuilt. This means it will not call onCreate method. It will just call onResume.

This is the normal behaviour.

However, on special situations, the system could destroy ActivityA (maybe because it needs memory to perform another task), so when you go back to it, the system will have to rebuild it.

To simulate this situation, there is a setting that you can check/uncheck, called "Don't keep activities".

If you have it checked, you will be simulating the situation explained above, it will always destroy the ActivityA as soon as it is not shown, and when you come back to it, the system will have to rebuild it calling onCreate.

enter image description here

Upvotes: 1

Enzokie
Enzokie

Reputation: 7415

It's not a typical scenario but when onCreate() is called when going back to that activity that means the Android OS kills it in the background.

Reason: Android is experiencing some memory shortage so killing some of the background task will be a must.

Is there any way to prevent that from happening?

No, you don't have a control over it, there are many reasons why its having a memory shortage e.g. other app installed that certain device is consuming more than expected. Although you can handle this use-case by storing the current information in onSaveInstanceState() and recovering the value from onCreate().

Upvotes: 1

Mike Lowery
Mike Lowery

Reputation: 2868

This is by design:

If an activity is paused or stopped, the system can drop it from memory either by asking it to finish (calling its finish() method), or simply killing its process. When the activity is opened again (after being finished or killed), it must be created all over.

See Activity Lifecycle. It's also why the Service class exists:

A Service is an application component that can perform long-running operations in the background and does not provide a user interface. Another application component can start a service and it will continue to run in the background even if the user switches to another application.

Upvotes: 1

aquaflamingo
aquaflamingo

Reputation: 822

Correct me if I'm wrong, but it should not call onCreate() here's a gross over simplification, but let's say activity's are managed much like a simple stack, let's call it AppStack

When a onCreate() for Activity A is called, the OS pushes the Activity Instance onto the AppStack

________ _________________
Activity|
___A____|_________________

When you click a button on Activity A, it launches a new intent to Activity B

Intent actB = new Intent(this, ActivityB.class);

and subsequently puts Activity A into Stopped state

When Activity B's onCreate() is called the OS pushes that Activity Instance onto the AppStack

________ __________________
Activity|Activity|
___A____|___B____|_________

Now if you call finish() or super.onBackPressed() in Activity B, the OS will pop() the Activity from the AppStack

________ __________________
Activity|
___A____|__________________

When the OS returns to the previous activity, it sees that it is Stopped and begins the process of Resuming it through onResume().

Now if there is some data that you require to be persistent, you can add it in by Overriding onResume()

Check out the activity lifecycle docs, for more info:

enter image description here

Upvotes: 3

Related Questions