I need a way to restore previous Android task when the current task (my application) is put in background using either back or overview button. For example, a video is played by youtube app when a SIP call is received. The softphone task is brought to foreground, then the call is answered. Once the call finishes, by pressing back or overview button youtube app is shown and the video continues playing. An example of such Android application is Linphone. I would like to know how this can be achieved programmatically.
3 Answers
As stated by others and here, Android handles it automatically for you. But if you need to add anything explicitly when going/coming to/from the background state then you can also override onSaveInstanceState()
and onRestoreInstanceState()
methods which will be called accordingly.
As your activity begins to stop, the system calls the
onSaveInstanceState()
method so your activity can save state information to an instance state bundle. The default implementation of this method saves transient information about the state of the activity's view hierarchy, such as the text in anEditText
widget or the scroll position of aListView
widget.To save additional instance state information for your activity, you must override
onSaveInstanceState()
and add key-value pairs to theBundle
object that is saved in the event that your activity is destroyed unexpectedly. If you overrideonSaveInstanceState()
, you must call the superclass implementation if you want the default implementation to save the state of the view hierarchy.
@Override
protected void onRestoreInstanceState(Bundle outState) {
if (outState != null) {
Crashlytics.log(1, "FormActivity", "Method:onRestoreInstanceState, Msg: saved instance is not null");
if (outState.containsKey("record")
&& Session.getCurrentRecord() == null) {
Session.setCurrentRecord(
gson.fromJson(
outState.getString("record"),
Record.class
)
);
}
if (outState.containsKey("user")
&& Session.getCurrentUser() == null) {
Session.setCurrentUser(
gson.fromJson(
outState.getString("user"),
User.class
)
);
}
}
super.onRestoreInstanceState(outState);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Session.setCurrentRecord(record);
outState.putString("record", gson.toJson(Session.getCurrentRecord()));
outState.putString("user", gson.toJson(Session.getCurrentUser()));
super.onSaveInstanceState(outState);
}

- 4,175
- 3
- 28
- 35
This is done automatically by the android system.
Now why you may not be able to notice this behavior for your app.
- You may be launching your app from the app launcher. Which means that you already put all other apps (except the launcher) in background. Now depending on the launcher settings you may go on page from where you launched the app or home when you press back button.
When can you observe this behavior
- If your activity is launched from background service, e.g. broadcast receiver
If your activity is launched by clicking on a notification button
- basically when your activity is created without killing or putting other apps in background, you will get back to the same app when your app is closed.
Exception - If you use home button all apps go to background and home screen appears.
hope this helps.

- 16,027
- 10
- 55
- 122
-
Just to clarify things: my application is the softphone, so I am interested in how to trigger the other app restore state (youtube app in the initial comment). – Cristea Bogdan Oct 25 '18 at 20:12
here is the official documentation on how to preserve the UI state:
https://developer.android.com/topic/libraries/architecture/saving-states
if the user configured the phone to always kill activities in background or they have limited resources then you have to handle that, but in some cases (your activity wasn't killed and remained in memory) as Mayank answered the system will do it for you.
getting a call from phone app will interrupt your app (System-initiated UI state dismissal) what you should do as suggested by the documentation above:
In the section: Managing UI state: divide and conquer
- Local persistence: Stores all data you don’t want to lose if you open and close the activity. Example: A collection of song objects, which could include audio files and metadata.
ViewModel: Stores in memory all the data needed to display the associated UI Controller. Example: The song objects of the most recent search and the most recent search query.
onSaveInstanceState(): Stores a small amount of data needed to easily reload activity state if the system stops and then recreates the UI Controller. Instead of storing complex objects here, persist the complex objects in local storage and store a unique ID for these objects in onSaveInstanceState(). Example: Storing the most recent search query.
so In your case have a view model that stores the Url and the video time when the call got received
and I would also store the same info in the instanceState using the proper life cycle hooks
here is a good SO thread with example on how to use the savedInstanceState : Saving Android Activity state using Save Instance State
it has old and new answers, you may want to read through it to get a sense of how things changed overtime
basically the three bullet points above are the recommended strategy by official documentation

- 1,014
- 1
- 11
- 19