无法使用onSaveInstanceState保存片段状态

时间:2019-08-05 21:03:09

标签: android android-fragments onsaveinstancestate

我有一个 MainActivity ,它管理两个 Fragments InputDataFragment (是应用程序开头显示的一个),其中包含一系列 EditText ResultsFragment 的内容,单击按钮后将替换 InputDataFragment
我想将数据保存在 EditText 字段中,以便即使在配置更改(例如屏幕旋转)后,如果我从 ResultsFragment 导航回到 InputDataFragment ,我可以使用这些数据再次填充它们。
这是我用来从 InputDataFragment 导航到 ResultsFragment 的代码:

@Override
public void onCalculate(String firstElement, String secondElement) {

    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    ResultsFragment resultsFragment = new ResultsFragment();
    fragmentTransaction.replace(R.id.container, resultsFragment);
    fragmentTransaction.commit();
}

这是我将值保存在 InputDataFragment

中的位置
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    super.onSaveInstanceState(outState);

    // Check if there's something to retain before putting it into outState Bundle
    if (!firstEditText.getText().toString().isEmpty()) {
        outState.putString("firstElement", firstEditText.getText().toString());
        Log.d(LOG_TAG, "Power saved");
    }

    if (!secondEditText.getText().toString().isEmpty()) {
        outState.putString("secondElement", secondEditText.getText().toString());
    }
}

这是我检索它们的地方

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

    if (savedInstanceState != null) {
        firstEditText.setText(savedInstanceState.getString("firstElement"));
        secondEditText.setText(savedInstanceState.getString("secondElement"));
    }
}

我已经尝试过执行关于这些Stack Overflow问题的已接受答案中建议的操作,保存并恢复我的Fragment引用:

Once for all, how to correctly save instance state of Fragments in back stack?

Using onSaveInstanceState with fragments in backstack?

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    getSupportFragmentManager().putFragment(outState, "dataFragment", dataFragment);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (savedInstanceState != null) {
        dataFragment = (InputDataFragment) getSupportFragmentManager().getFragment(savedInstanceState, "dataFragment");
    } else {
        dataFragment = new InputDataFragment();
    }

    fragmentTransaction.add(R.id.container, dataFragment);
    fragmentTransaction.commit();
}

Fragment 中调用onActivityCreated时,这些值正确显示在 Bundle 中,因为我尝试记录它们

问题是当我在 ResultFragment 中并且旋转设备时,在putFragment中调用onSaveInstanceState的行中出现错误。

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mycompany.myproject, PID: 20161
java.lang.IllegalStateException: Fragment InputDataFragment{679fd26} is not currently in the FragmentManager
    at androidx.fragment.app.FragmentManagerImpl.putFragment(FragmentManager.java:923)
    at com.mycompany.myproject.MainActivity.onSaveInstanceState(MainActivity.java:21)
    at android.app.Activity.performSaveInstanceState(Activity.java:1646)
    at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1508)
    at android.app.ActivityThread.callActivityOnSaveInstanceState(ActivityThread.java:5476)
    at android.app.ActivityThread.callActivityOnStop(ActivityThread.java:4792)
    at android.app.ActivityThread.handleRelaunchActivityInner(ActivityThread.java:5424)
    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:5342)
    at android.app.servertransaction.ActivityRelaunchItem.execute(ActivityRelaunchItem.java:69)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2155)
    at android.os.Handler.dispatchMessage(Handler.java:109)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:7539)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:958)

1 个答案:

答案 0 :(得分:0)

我解决了,问题出在这里

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

if (savedInstanceState != null) {
    dataFragment = (InputDataFragment) getSupportFragmentManager().getFragment(savedInstanceState, "dataFragment");
} else {
    dataFragment = new InputDataFragment();
}

fragmentTransaction.add(R.id.container, dataFragment);
fragmentTransaction.commit();
}

当我进入 ResultsFragment 并旋转设备时,将调用Activity onCreate方法。即使我从Bundle中获得了先前的 InputDataFragment ,调用FragmentTransaction.add()也会导致数据丢失。

FragmentTransaction代码移动到解决方案为 savedInstanceState == null 的情况下。