忽略navigation()调用:FragmentManager已保存其状态

时间:2018-08-17 12:15:02

标签: android android-architecture-components fragmentmanager android-jetpack android-architecture-navigation

我在 ---- OpenCommand: Entered 39 FFInterop.Initialize: FFmpeg v4.0 0 EVENT START: MediaInitializing 0 EVENT DONE : MediaInitializing 379 EVENT START: MediaOpening 0 EVENT DONE : MediaOpening 0 COMP VIDEO: Start Offset: 0,000; Duration: N/A 41 SYNC-BUFFER: Started. 609 SYNC-BUFFER: Finished. Clock set to 1534932751,634 0 EVENT START: MediaOpened 0 EVENT DONE : MediaOpened 0 EVENT START: BufferingStarted 0 EVENT DONE : BufferingStarted 0 OpenCommand: Completed 0 V BLK: 1534932751,634 | CLK: 1534932751,634 | DFT: 0 | IX: 0 | PQ: 0,0k | TQ: 0,0k 0 Command Queue (1 commands): Before ProcessNext 0 Play - ID: 404 Canceled: False; Completed: False; Status: WaitingForActivation; State: 94 V BLK: 1534932751,675 | CLK: 1534932751,699 | DFT: 24 | IX: 1 | PQ: 0,0k | TQ: 0,0k 中使用navigation,然后我开始MainActivity(出于结果)。 SecondActivity完成后,我想继续在SecondActivity中导航,但是MainActivity已经保存了他的状态。

FragmentManager上,我收到日志消息:

Navigation.findNavController(view).navigate(R.id.action_next, bundle)

我如何继续导航?

5 个答案:

答案 0 :(得分:2)

您必须始终在活动的super.onActivityResult()中致电onActivityResult。那是什么:

  1. 解锁片段,以便他们可以执行片段事务(即避免出现state is already saved错误)

  2. 向调用onActivityResult的Fragment发送startActivityForResult回调。

答案 1 :(得分:0)

我已经通过这种方式解决了这个问题:

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

然后

    @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);
    }
}

不确定,如果这不是可怕的破解,但对我有用。此时,FramgentManager的状态已还原(onResume),并且导航没有问题。

答案 2 :(得分:0)

最后,我通过导航到恢复状态之前的简单调用super.onPostResume()来解决此问题。

答案 3 :(得分:0)

我认为上述解决方案应该有效。但是我的问题不同。有一个第三方sdk正在使用我提供的上下文来启动其活动,并将结果传递给我必须实现的侦听器。

所以我没有选择使用onActivityResult的方法:(

我使用以下技巧来解决该问题:

   private var runnable: Runnable? = null // Runnable object to contain the navigation code

    override fun onResume() {
        super.onResume()

        // run any task waiting for this fragment to be resumed
        runnable?.run()
    }


    override fun responseListener(response: Response) {    // Function in which you are getting response

        if (!isResumed) {    
            // add navigation to runnable as fragment is not resumed
            runnable = Runnable {
                navController.navigate(R.id.destination_to_navigate)
            }
        } else {
            // navigate normally as fragment is already resumed
            navController.navigate(R.id.destination_to_navigate)
        }
    }


请让我知道是否有更好的解决方案。目前,我发现这非常简单且易于实现:)

答案 4 :(得分:0)

在导航之前调用super.onPostResume()。。。