简体   繁体   中英

savedInstanceState is not null but has no data

I have a fragment activity where I navigate to a search page, then back to the list with the result. Sometimes the activity needs to be re-created, so I am saving my instance data like so:

@Override
public void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);

    System.out.println("Saving instance state!");

    // Save the instance data
    // Filter data
    Intent mIntent = new Intent(this, BasicMapDemoActivity.class);
    filter.addFilterToIntent(mIntent);
    outState = mIntent.getExtras();

    // Save date data
    int year = currentDate.getYear();
    int month = currentDate.getMonth();
    int day = currentDate.getDay();
    outState.putInt("YEAR",year);
    outState.putInt("MONTH",month);
    outState.putInt("DAY",day);

    // Location data
    double latitude = mapLocation.latitude;
    double longitude = mapLocation.longitude;
    outState.putDouble("LATITUDE",latitude);
    outState.putDouble("LONGITUDE",longitude);

    // Other
    outState.putString("VIEW_MODE",viewMode.toString());
    outState.putString("SORT_TYPE",sortType);
    outState.putInt("ZOOM", mapZoom);
}

Then I am attempting to restore the data in onCreate, but when I get there, the bundle is not null, but it does not contain any of the data I saved to it. All of the following get functions return zero (I confirmed that the data was non-zero at the end of the above code block):

Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    // If we are 're-creating', then get the old instance data, otherwise set defaults
    if(savedInstanceState == null)
    {
        // Set up default instance data
        currentDate = new Date(115,4,1);
        filter = new ObajoFilter();
        filter.start = currentDate;
        filter.end = new Date(115,4,1,23,59,0);
        viewMode = ViewMode.MAP;
        sortType = "Distance";
        mapLocation = userLocation;
        mapZoom = 16;

    } else {

        // Get date data
        int year = savedInstanceState.getInt("YEAR");
        int month = savedInstanceState.getInt("MONTH");
        int day = savedInstanceState.getInt("DAY");
        currentDate = new Date(year,month,day);

Any ideas on what could be emptying out the bundle? From what I understand, the savedInstanceData is only empty once the app is completely destroyed.

Because of this line outState = mIntent.getExtras();

Your outState points to different address mIntent.getExtras() , So it's empty

You should use outState.putXX directly without creating new object.

Are you storing the information that you want to retain in the onSaveInstanceState something like the following

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putParcelable("someobject", someobject);
    outState.putBoolean("someboolean", someBoolean);
    outState.putInt("someint", someInt);
    super.onSaveInstanceState(outState);
}

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