Ignoring navigate() call: FragmentManager has already saved its state

為{幸葍}努か 提交于 2019-12-12 09:38:25

问题


I'm using navigation in MainActivity, then I start SecondActivity (for result). After finish of SecondActivity I would like to continue with navigation in MainActivity, but FragmentManager has saved his state already.

On Navigation.findNavController(view).navigate(R.id.action_next, bundle) I receive log message:

Ignoring navigate() call: FragmentManager has already saved its state

How I can continue in navigation?


回答1:


You must always call super.onActivityResult() in your Activity's onActivityResult. That is what:

  1. Unlocks Fragments so they can do fragment transactions (i.e., avoid the state is already saved errors)

  2. Dispatches onActivityResult callbacks to Fragments that called startActivityForResult.




回答2:


Finally, I fix the issue by simple calling super.onPostResume() right before navigating to restore state.




回答3:


I've solved this problem this way:

    @Override
public void onActivityResult() { //inside my fragment that started activity for result
        model.navigateToResults = true; //set flag, that navigation should be performed
}

and then

    @Override
public void onResume() { //inside fragment that started activity for result
    super.onResume();

    if(model.navigateToResults){
        model.navigateToResults = false;
        navController.navigate(R.id.action_startFragment_to_resultsFragment);
    }
}

not sure, if this is not a terrible hack, but it worked for me. FramgentManager state is restored at this point (onResume) and no problems with navigation occur.



来源:https://stackoverflow.com/questions/51895243/ignoring-navigate-call-fragmentmanager-has-already-saved-its-state

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!