Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANDROID: Activity state after pressing the back button

Imagine you have the following sequence of activities:

Activity A -> Activity B -> Activity C

When you are on Activity C, pressing the native back button, takes you to Activity B. Now what is the state of Activity C? Is it still in memory or it has been finished?

If it is still in the memory, is there a way to resume the activity? Other than starting another instance of this activity...

I should add that this is the standard case where you do not use any flags including: FLAG_ACTIVITY_CLEAR_TOP

like image 570
Kasra Avatar asked Mar 09 '14 07:03

Kasra


People also ask

What happens to activity when back button is pressed?

However, when the back button is pressed, the activity is destroyed, meaning, the temporary data is lost during this call.

What happens to activity when home button is pressed?

Instead if the Home button is pressed, the app moves to the Stopped state, still running in the background. Clearing all the apps with the Clear All button may or may not improve the overall performance because there might be no apps running in the background.

How can you tell if someone pressed back button on Android?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library.

How do I skip back pressed activity on Android?

In Android we can change the default behaviour of the any activity like keypress, back button press, swap left swap right, to do Disable Back Press we need to override default functionality of onBackPressed() or onKeyDown() method in Activity class.


1 Answers

Default behavior is that when you press hardware "back" button, current activity will be removed from the backstack and activity "destroy" sequence will be initiated. From that moment you should not rely on the fact that it might be somewhere around - it is all up to Android to decide when does it actually kill this activity.

What my previous investigations show is that victim's onDestroy() will be called only when new activity is done loading and is idle.

You can specify android:launchMode="singleInstance" for your activity in Manifest. This will ensure that only one instance of activity is created at the time

like image 83
Pavel Dudka Avatar answered Sep 22 '22 15:09

Pavel Dudka