简体   繁体   English

savedInstanceState不为null,但没有数据

[英]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. 然后,我尝试还原onCreate中的数据,但是当我到达那里时,捆绑包不为null,但是它不包含我保存到其中的任何数据。 All of the following get functions return zero (I confirmed that the data was non-zero at the end of the above code block): 以下所有get函数均返回零(在上述代码块的末尾,我确认数据为非零):

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. 据我了解,仅当应用程序完全销毁后,savevedInstanceData才为空。

Because of this line outState = mIntent.getExtras(); 由于这一行, 所以outState = mIntent.getExtras();

Your outState points to different address mIntent.getExtras() , So it's empty 您的outState指向不同的地址mIntent.getExtras() ,因此为空

You should use outState.putXX directly without creating new object. 您应该直接使用outState.putXX而不创建新对象。

Are you storing the information that you want to retain in the onSaveInstanceState something like the following 您是否要在onSaveInstanceState存储要保留的信息,如下所示

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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