我在还原嵌套片段的状态时遇到问题。这是层次结构:

MainActivity

----> AlarmOverviewFragment(实际上没有保存任何重要数据)

--------> AlarmFragment(保存数据)

我遇到的问题是,当我最小化应用程序(按“主页”按钮)然后重新打开应用程序时,每次尝试从作为Fragment实例变量之一存储在。

令人沮丧的是,我将实例变量保存在onCreateView()中,但是onSaveInstanceState()savedInstanceStateonCreateView()onCreate()中始终为null。

显然,我在这里错过了一些巨大的东西,因为我尝试了许多关于stackoverflow的解决方案,但无济于事。以下是一些相关的代码位:

保存实例数据:

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putInt("hour", this.hour);
    outState.putInt("window", this.window);
    outState.putInt("minutes", this.minutes);
    outState.putBooleanArray("days", this.days);
    super.onSaveInstanceState(outState);
}


尝试还原数据失败

@Override
public void onCreate(Bundle savedInstanceState) {

    if (savedInstanceState != null) {
        /* Never happens */
        hour = savedInstanceState.getInt("hour");
        window = savedInstanceState.getInt("window");
        minutes = savedInstanceState.getInt("minutes");
        days = savedInstanceState.getBooleanArray("days");
    }
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        /* Also never happens */
    }
    super.onActivityCreated(savedInstanceState);
}


为了以防万一,这是堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: group12.wakemeup, PID: 22756
                  java.lang.RuntimeException: Unable to resume activity {group12.wakemeup/group12.wakemeup.MainActivity}: java.lang.NullPointerException: Attempt to read from null array
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3121)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5525)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                   Caused by: java.lang.NullPointerException: Attempt to read from null array
                      at group12.wakemeup.AlarmFragment.onCreateView(AlarmFragment.java:113)
                      at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)
                      at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)
                      at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                      at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1682)
                      at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
                      at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
                      at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)
                      at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
                      at android.app.Activity.performStart(Activity.java:6288)
                      at android.app.Activity.performRestart(Activity.java:6334)
                      at android.app.Activity.performResume(Activity.java:6339)
                      at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3110)
                      at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3152) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5525) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 


编辑

额外的信息

每个onActivityCreated()是动态添加的,并通过AlarmFragment指定了唯一的ID,因此我不确定如何将每个片段保存在View.generateViewId()中。还值得一提的是,我确实使用MainActivity来处理选项卡/刷卡操作(此应用程序中还有其他Overview片段,但我将它们省略了,因为它们不会给我带来麻烦)。我听说有一种使用FragmentPagerAdapter保存状态的方法,也许还有一种使用ViewPager的状态?

最佳答案

由于问题似乎与开始时有所不同,因此实际上您不必每次都在onNewIntent中重新创建片段。
您可以改为:

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if ("some_action".equals(intent.getAction())) {
        //your action, in your case create and assign fragment

        //remove the action
        intent.setAction(null);
    }
}

07-24 09:48
查看更多