(Original) Solve some pits when APP process is killed

In the past few days, I encountered such a problem in development.
When the number of open apps reaches a certain number, the
original App process will be killed by the system,
and then when the killed App is entered again,
some abnormalities are found.
After investigation, it is the getitem of viewpager. The method is not called.
According to some information on the Internet, there will be some problems in
using Android's default recovery method to restore the data
. For
example, the value returned by the fragment's getActivity is empty.
So I thought about
whether it is possible to not perform the reloading method?
So there is this blog.

The processing method is actually very simple,
that is, make a judgment in the activity's onCreate method. The
code is as follows 

@Override
protected void onCreate(Bundle savedInstanceState) {

    if (savedInstanceState != null) {       
        savedInstanceState = null;  
    }

    super.onCreate(savedInstanceState);

}

Note here. It
must be executed before the super.onCreate function.
This is to directly empty the bundle passed after the system is saved.
If your reason is fragment-related, you can
replace it with this code directly without such an extreme approach. :
SavedInstanceState.remove("android:support:fragments");
But note that
if your Activity class inherits Activity instead of FragmentActivity,
the parameter inside is android:fragments

Speaking of recovering data, one more point.
We all know that Android uses these two methods to recover data.

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

And the Bundle parameter of oncreate

A new attribute has been added to Activity after API 21.
Just set the attribute
android:persistableMode="persistAcrossReboots" in the activity in the Manifest.
Activity has the ability to persist, and it needs to cooperate with a new bundle, that is, PersistableBundle . 
What does that mean?
It is used for data recovery after the machine is shut down.
These two methods are overloads of the above two methods. The
code is as follows

  @Override
  public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) 
  {
      super.onSaveInstanceState(outState, outPersistentState);
  }

  @Override
  public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle 
  persistentState) {
      super.onRestoreInstanceState(savedInstanceState, persistentState);
  }

We can see more than a Bundle
after bundle the data in the shutdown restart is open there will be
on these two methods, we can refer to this blog
https://blog.csdn.net/fenggering/article/details/53907654
addition , I also thank this blog for allowing me to solve the problem in development
https://blog.csdn.net/brycegao321/article/details/52061853

Guess you like

Origin blog.csdn.net/Android_xiong_st/article/details/84954741