Management reboot Android activity after the shutdown process

My application runs on a special device that is running a custom build Android Gingerbread 2.3.7

There are conditions under which the system will stop my application. I assume that the device manufacturer is considering these emergencies when all third-party applications should be immediately turned off, the device can carry out its core tasks.

I can duplicate the behavior that I see on the device using the emulator, and the DDMS, choosing my task and pressing the "Stop Process" button. That's the behavior that I see.

My application usually performs four actions: Activity A starts activity B, B launches the Activity C, and C starts the activity of D. Thus, when the activity D works on top of my stack:

A - B - C - D

If at this point the process ends, step D does not receive onPause () call or onStop (). It is not possible to save its state.

After the process is dead, Android ActivityManager launches a new challenge for my application and runs Activity C. I think that this is a standard Android behavior to reset the broken application.

My question is: can I control this behavior is restarted? If Android starts to restart my application, I need to restore the stack activity, Activity C does not really make sense to run autonomously (pressing "Back" to exit from the application, and it does not make sense for this action).

Can I prevent the restart? Can I restart my steps in the sequence? Can I restart only the beginning of activity of A?

I found this interesting discussion , which is, I think, explains why Activity C restarts, not the activity of D.

As for the restart of activity - if the process is performing the activities of the foreground disappears, the system discards if it does not have a valid saved state (usually that means that he stopped and gave the system the result onSaveInstanceState to pause). As soon as he decided whether to drop this activity or not, it will resume the activity is now at the top of the stack. If this is one of your activities - or because you have other, crashed, or the one that crashed, was somehow established a pause state - then it will restart your process to show that the top activities.

And some of these issues, such as Prevent restoration activity of the stack? and this interesting thread

+6
source share
1 answer

After much experimentation, I chose the approach outlined in the "Developer All Android> " Active Fault restart after OS kills " One exchange Q / A was.:

Question:

"Went through the code and see what you are talking about. It is onSaveInstanceState and stores the data in the Bundle. Will Bundle this information available when you restart the activity after the OS kills the process?"

Answer:

". It should be you get this Bundle in onCreate () and onRestoreInstanceState () onCreate () can be passed null for the Bundle, Bundle if not to restore onRestoreInstanceState () -.. Is called only when the restoration of the package."

I moved all the session data needed to recover after the application was killed in a serializable singleton.

In onSaveInstanceState () I put the serialized data in a session savedInstanceState package

@Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); // save and restore session data to instance state to recover from task termination savedInstanceState.putSerializable(SessionVariables.class.getName(), mSessionVariables); } 

The onCreate () and onRestoreInstanceState () I check whether my singleton instance for the session. If it does not have reliable data, I restore session variables from the package savedInstanceState and put the restored object as my singleton session variable.

 @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (mSessionVariables.propertyThatShouldbeGood == null || mSessionVariables.propertyThatShouldbeGood .length() == 0) { // save and restore session data to instance state to recover from task termination Serializable serializedSessionVariables = savedInstanceState.getSerializable(SessionVariables.class.getName()); if (serializedSessionVariables != null) { mSessionVariables = (SessionVariables) serializedSessionVariables; SessionVariables.putInstance(mSessionVariables); } } } () == @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (mSessionVariables.propertyThatShouldbeGood == null || mSessionVariables.propertyThatShouldbeGood .length() == 0) { // save and restore session data to instance state to recover from task termination Serializable serializedSessionVariables = savedInstanceState.getSerializable(SessionVariables.class.getName()); if (serializedSessionVariables != null) { mSessionVariables = (SessionVariables) serializedSessionVariables; SessionVariables.putInstance(mSessionVariables); } } } state to recover from task termination @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (mSessionVariables.propertyThatShouldbeGood == null || mSessionVariables.propertyThatShouldbeGood .length() == 0) { // save and restore session data to instance state to recover from task termination Serializable serializedSessionVariables = savedInstanceState.getSerializable(SessionVariables.class.getName()); if (serializedSessionVariables != null) { mSessionVariables = (SessionVariables) serializedSessionVariables; SessionVariables.putInstance(mSessionVariables); } } } ); @Override public void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); if (mSessionVariables.propertyThatShouldbeGood == null || mSessionVariables.propertyThatShouldbeGood .length() == 0) { // save and restore session data to instance state to recover from task termination Serializable serializedSessionVariables = savedInstanceState.getSerializable(SessionVariables.class.getName()); if (serializedSessionVariables != null) { mSessionVariables = (SessionVariables) serializedSessionVariables; SessionVariables.putInstance(mSessionVariables); } } } 

Now that my task would be killed, Android restores the previous operation of the stack. onCreate () function restores all the session data from a saved copy of the packet conditions.

"Back" button also works fine at this stage, so I made a mistake with Android, without saving the stack activity. I think that it simply restores the back steps as needed (when you move back). If you do not return back, it does not create them. At least, it looked like a HierarchyViewer.

When my stack task ABCD and killed, "C" operation is restored and "D" is lost. But now "C" is in a healthy state, and the user can easily return to "D". Now I can also start a "D" activity automatically with "C", I just have not had time.

+3
source

All Articles