重新开始活动后重置海军图

发布于 2025-02-10 03:28:09 字数 295 浏览 3 评论 0原文

我有一个带有2个片段的导航图,片段A和片段B。 片段A是我的开始目的地。

如果我在Fragment B中,并且打开应用程序设置,并撤销许可,我会看到:

  1. 重新创建活动,但是我看不到ondestory被调用,
  2. 而不是使用Fragment A启动活动,该活动是从片段开始的。

我还看到该应用程序已重新创建并直接打开活动B,而不是从活动A开始(旧代码,我们几乎没有活动)

可以以某种方式重置导航图,以便它会以某种方式重置从碎片开始?

谢谢。

I've a navigation graph with 2 fragments, Fragment A and Fragment B.
Fragment A is my start destination.

If I'm in Fragment B and I open the app settings, and revoke a permission I see that:

  1. activity is recreated, but I don't see onDestory is called
  2. instead of starting the activity with Fragment A, the activity is started with Fragment B.

I also see that the application is recreated and opens directly Activity B instead of starting from Activity A (old code, we have few activities)

is there a way to reset the navigation graph somehow so it will start from Fragment A?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千鲤 2025-02-17 03:28:09

遇到您的问题,寻找类似问题的答案。

就我而言,我有一个单一的活动应用程序,该应用在更改许可状态时在片段B中,并且在重新启动活动后,在片段B中重新打开。像您一样,我希望我的活动从片段A开始,这也是我的NAV图的开始或默认目的地。

我解决的问题的方法是,由于conical_permission_change ,我都会检测到,然后我将savedinstancestate内部无效。我的活动的oncreate()

这里有一些代码:

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(
            when (requiresPermissionChangeRestart()) {
                true -> {
                    Log.e(TAG "Resetting Activity after permissions manual change")
                    null
                }
                false -> {
                    savedInstanceState
                }
            }
        )
    }
...
    private fun requiresPermissionChangeRestart(): Boolean = (getSystemService(Context.ACTIVITY_SERVICE)
            as ActivityManager).let { am ->
        am.getHistoricalProcessExitReasons(null, 0, 0)
            .find {
                it.reason == ApplicationExitInfo.REASON_PERMISSION_CHANGE
            }
            .run {
                when (this != null) {
                    true -> {
                        Log.w(TAG, "Permissions for package $packageName where changed by the user")
                        true
                    }
                    false -> false
                }
            }
    }

}

这可能是一种黑客,但是这个小技巧可能会帮助您为特定问题提供更合适的解决方案。只要您从给定的活动中摆脱SAVEDINSTANCESTATE,您就应该能够按照期望它的方式重新启动 RESTART

Ran into your question looking for an answer for a similar problem.

In my case, I have a single Activity app which is in Fragment B when the permission state is changed, and also reopens in Fragment B after the Activity is restarted. Like you, I want my Activity to start with Fragment A, which is also the start or default destination of my nav graph.

The way I solved my problem was that, I would detect whenever the Activity had been exited because of a REASON_PERMISSION_CHANGE, then I nullify the savedInstanceState inside my Activity's onCreate().

Here's some code:

// MainActivity.kt
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(
            when (requiresPermissionChangeRestart()) {
                true -> {
                    Log.e(TAG "Resetting Activity after permissions manual change")
                    null
                }
                false -> {
                    savedInstanceState
                }
            }
        )
    }
...
    private fun requiresPermissionChangeRestart(): Boolean = (getSystemService(Context.ACTIVITY_SERVICE)
            as ActivityManager).let { am ->
        am.getHistoricalProcessExitReasons(null, 0, 0)
            .find {
                it.reason == ApplicationExitInfo.REASON_PERMISSION_CHANGE
            }
            .run {
                when (this != null) {
                    true -> {
                        Log.w(TAG, "Permissions for package $packageName where changed by the user")
                        true
                    }
                    false -> false
                }
            }
    }

}

It might be kind of a hack, but this little trick might help you arrive at a more fitting solution for your particular problem. As long as you get rid of the savedInstanceState from the given Activity when it matters, you should be able to restart your Activity the way you would expect it to do so.

っ左 2025-02-17 03:28:09

当活动将重新创建活动时,此代码将重置NAV图。

override fun onStart() {
    super.onStart()
    navController.setGraph(R.navigation.your_nav_graph)
}

This code will reset nav graph when the activity will recreate.

override fun onStart() {
    super.onStart()
    navController.setGraph(R.navigation.your_nav_graph)
}
岁月流歌 2025-02-17 03:28:09

我最终这样做:
在on Create中:

savedInstanceState?.let {
        if (!ConfigChange) {
            val fragment: Fragment? =
                supportFragmentManager.findFragmentById(R.id.my_nav_host)
            fragment.let {
                if (it is NavHostFragment) {
                    it.navController.navigate(R.id.fragmentA)
                }
            }
        }
    }

I ended up doing it this way:
in onCreate:

savedInstanceState?.let {
        if (!ConfigChange) {
            val fragment: Fragment? =
                supportFragmentManager.findFragmentById(R.id.my_nav_host)
            fragment.let {
                if (it is NavHostFragment) {
                    it.navController.navigate(R.id.fragmentA)
                }
            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文