简体   繁体   中英

Really how onRestoreInstanceState() works?

I need your help. I'm making a application and I use the methods onSaveInstanceState() and onRestoreInstanceState() , but second method doesn't work.

I can see how the program accesses to onSaveinstancestate() when the home button is pushed, but when I return to the application the code doesn't call onRestoreInstanceState() or onCreate() .

As a result, the application start from scratch. I don't know the reason... can you help me?

This is my code:

public class MainActivity extends ActionBarActivity {

MyView myView;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myView = new MyView(this);
        setContentView(myView);

    if (savedInstanceState != null) {
        myView .SetScore(savedInstanceState.getInt("Id"));
        myView .SetNivel(savedInstanceState.getInt("Valor"));  
    }
} 

.....

 @Override
    protected void onRestart(){
        super.onRestart();
        myView = new MyView(this);
        setContentView(myView );
    }

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState != null) {
        myView.SetScore(savedInstanceState.getInt("Id"));
        myView.SetNivel(savedInstanceState.getInt("Valor"));
   }
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putInt("Score",myView.GetId());
    savedInstanceState.putInt("Nivel",muView.GetValor());
    super.onSaveInstanceState(savedInstanceState);
}
}

As the system begins to stop your activity, it calls onSaveInstanceState() so you can specify additional state data you'd like to save in case the Activity instance must be recreated. If the activity is destroyed and the same instance must be recreated, the system passes the state data defined to both the onCreate() method and the onRestoreInstanceState() method.

You can check this https://developer.android.com/training/basics/activity-lifecycle/recreating.html

You can see what happens with a Toast inside your method.

And check this https://stackoverflow.com/a/4967491/3653989

onRestoreInstanceState(...) will only be called if the activity was destroyed while it was in the background. You can force this to happen if you turn on the "don't save activities" developer option.

But this doesn't explain why your app seems to restart from scratch. Are you sure you're restoring the data correctly? Add some logging in onRestoreInstanceState(...) to make sure it's being called and doing the right thing. And are you sure you're not calling finish() somewhere you shouldn't be, like onPause() ?

Try to use a fragment with setRetainInstance(true).

Edited

Declare all your UI in Fragment( http://developer.android.com/guide/components/fragments.html ) like you do it in Activity.

public static class ExampleFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup Bundle savedInstanceState) {
        setRetainInstance(true);
        return new MyView(getActivity());
    }
}

public static class MainActivity extends ActionBarActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        getSupportFragmentManager().beginTransaction().add(new ExampleFragment(), R.id.layout_where_you_want_to_display_fragment).commit();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM