2

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);
}
}
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
Scleyth
  • 23
  • 3
  • This question may help you http://stackoverflow.com/questions/5574462/why-onrestoreinstancestate-never-gets-called – Prudhvi Feb 09 '15 at 01:05
  • Have you checked that they are not called? In your code, you save with different keys then you use for restoring. – StenSoft Feb 09 '15 at 01:17
  • When you press HOME and then return to your application, it should still be in the same state as when you left it , therefore there is no reason to call `onRestoreInstanceState()`. What is the problem? – David Wasser Feb 09 '15 at 11:11

3 Answers3

1

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

Community
  • 1
  • 1
Adrian Cid Almaguer
  • 7,815
  • 13
  • 41
  • 63
0

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()?

Kevin Krumwiede
  • 9,868
  • 4
  • 34
  • 82
-2

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();
    }
}
Stepango
  • 4,721
  • 3
  • 31
  • 44