Why is onSaveInstanceState called when starting new activity? - android

Why is onSaveInstanceState called when starting new activity with startActivity(intent), but when user presses back or home from new activity, onRestoreInstanceState is not called?

When you navigate from Activity A to Activity B, Activity A will have its onSaveInstanceState() method invoked in case Activity A is destroyed (because the system needs to free up resources).
When you navigate back from Activity B to Activity A, there are a few different paths the code can take. If Activity A is still up and running, it will simply resume (and you'll see onResume() called). If instead Activity A was destroyed by the system while you were on Activity B, Activity A will need to be recreated. You'll see onCreate() called by the system (and its param savedInstanceState will be non-null and populated with what you put into it in onSaveInstanceState()) as well as any other lifecycle methods that happen "after" onCreate().

Related

Example for activity that starts another activity but is still visible

I just read the following from Android Developers docs:
Here's the order of operations that occur when Activity A starts
Activity B:
Activity A's onPause() method executes.
Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
Then, if Activity A is no longer visible on screen,
its onStop() method executes.
I just want to know when an activity can start another and still be visible(a concrete example). (because it means that onStop() is always calls when I switch activities)
You can start an Activity B like a dialog if you specify a flag in AndroidManifest.xml:
<activity android:name=".ActivityB"
android:theme="#android:style/Theme.Holo.Light.Dialog"/>
Then Activity B will not take the full screen and you will still see the underlying activity.
onStop() won't be called if you previous activity is visible to user.
onStop will be called only after previous activity is completely invisible to user.
So if new activity is dialog or some transparent background, then previous activity will be displayed to user and onStop() of previous activity won't be called.

Return to previous Activity without deleting current state - Android

I wrote an Android app with several Activities and a Main Activity. When I go from the Main Activity to lets say Activity B, I want to "pause" the Main Activity, when the back button is pressed it should go back and reactivate the Main Activity instead of calling onCreate().
This shall work with all Activities, so if I click on a button to start Activity B, it shall also reactivate the old Activity B instead of creating a new state with onCreate().
How can I realize this?
PS: I already tried it with parcelable, but this do only work, if I close the application or something unexpected happens.
It's always possible that the first activity may be destroyed when you start another activity. It will be recreated when you go back to it. Every app should be written with this possibility in mind. To make sure your activities can handle being destroyed and recreated, turn on the "don't keep activities" developer option.
It is by default in Android. If you Start Activity B from Activity A then activity A goes in stopped state. Below methods will be called of Activity A
onPause()
onStop()
When you tap on Back key on Activity B. Below methods of Activity A will be called.
onRestart()
onStart()
onResume()
For reactivating Activity B, you can set Activity B launch mode as singleInstance. This will make sure that only single instance of Activity B will be created. onCreate will be not be called again. onNewIntent will be called when that activity is reactivated.
Refer: http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
Activities live in stack order. Each activity has its life cycle. When you close an activity (Activity B in your example) it eventually reaches onDestroy() method and removed from the stack order. It is by default in Android and there's nothing you can do about it.
What you can do is rewrite the onStop() method in Activity B and save some activity data (like text in EditText, for example) in SharedPreferences - read here. Then in your onCreate() method you can call for SharedPreferences, check if it's not empty and restore the text in the EditText by pulling appropriate value by key.

Activity starting another activity. Guarantee that the first activity's onPause will be called before second's onResume

I have an Activity A and Activity B. Activity A starts Activity B.
Is there a guarantee that A's onPause() will always be called before calling B's onResume()?
This is related to this entry
Found the answer here:
When activity B is launched in front of activity A, this callback will
be invoked on A. B will not be created until A's onPause() returns, so
be sure to not do anything lengthy here.
onPause ()
When activity B is launched in front of activity A, this callback will be invoked on A. B will not be created until A's onPause() returns, so be sure to not do anything lengthy here
http://developer.android.com/reference/android/app/Activity.html#onPause()
Yes. Activity A's onPause() will be called before passing to Activity B's onResume() if the navigation is not the first time. If navigating to Activity B is for the first time, then Activity B's onCreate() will be called after Activity A's onPause(). Take a look at Activity's Lifecycle for clear understanding.

Android SDK launch same instance of activity

I need to launch the same instance of an activity even if the user goes back and forth with the navigation. The user navigates in a stack of different activies (A, B, C), but when he goes to one of these activities it will show the same instance of that activity (like a static activity) calling only onResume.
You can't force your Activity to start up in onResume(). What you can do, however, is save your Activity's state to a Bundle in onPause() and onStop(). Then, in onStart() read this Bundle to get your Activity's state.
The closest you can get to this is to use FLAG_ACTIVITY_REORDER_TO_FRONT in the Intent you use with startActivity().
However, this will call more than onResume(). At minimum, your activities will be called with onRestart() and onStart(), assuming that whatever was in the foreground took over the whole screen.

Life cycle called when backpress is pressed to navigate back to the previous activity?

What activity state is called when another activity, in the same application, is launched, and then the backpress button is clicked to navigate back to it?
What lifecylce method is called during the proccess of going back to the previous activity?
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
What lifecylce method is called during the proccess of going back to
the previous activity?
According to the docs: onPause(), onStop(), and possibly onDestroy(). In addition to the lifecycle documentation, you might want to read the docs on Tasks and Back Stack.

Categories

Resources