简体   繁体   中英

Android onSaveInstanceState is always null

Consider the following method:

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putSerializable("repertoire",rep);
}

I'm using the menu NavigationDrawer geneated by Android Studio. When I go back to main activity, savedInstanceState is always null in my class:

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks :

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if( savedInstanceState != null ) {
        rep = (repertoire) savedInstanceState.getSerializable("repertoire");
            }
        setContentView(R.layout.activity_main);

        mNavigationDrawerFragment = (NavigationDrawerFragment)                      getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
        mTitle = getTitle();
        ListView vue = (ListView) findViewById(R.id.listView);
        if(rep==null){
            rep = new repertoire();
        }

        // Set up the drawer.
        mNavigationDrawerFragment.setUp(
                R.id.navigation_drawer,
                (DrawerLayout) findViewById(R.id.drawer_layout));
}

I'm not 100% sure, but shoudln't you restore you activity in the onRestoreInstanceState()? Again I'm not 100% sure but it might work.

Go ahead and create it!

@Override
public void onSaveInstanceState(Bundle outState) {
    if (outState == null) {
        outState = new Bundle();
    }

    outState.putSerializable("repertoire", rep);

    super.onSaveInstanceState(outState);
}

The super method doesn't do anything unless a non-null Bundle is passed to it and its super method is just a stub! The view hierarchy is saved separately. You'll now receive the Bundle as a parameter in both onCreate(Bundle) and onRestoreInstanceState(Bundle) .

My current code: In onCreate :

                  @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
              if( savedInstanceState != null ) {
                  System.out.println("back");
                  rep = (repertoire) savedInstanceState.getSerializable("repertoire");
              }

and :

                @Override
        public void onSaveInstanceState(Bundle outState) {
            System.out.println("bibi");
            if (outState == null) {
                outState = new Bundle();
            }
            super.onSaveInstanceState(outState);
            outState.putSerializable("repertoire", rep);


        }
            @Override
            public void onRestoreInstanceState(Bundle bundle){
                System.out.println("okm");
                super.onRestoreInstanceState(bundle);
                bundle.putSerializable("repertoire",rep);
            }

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