【问题标题】:How to return from activity to a specific fragment in navigation bottom如何从活动返回到导航底部的特定片段
【发布时间】:2021-05-11 16:33:20
【问题描述】:

大家好, 我正在开发一个需要你帮助的安卓应用程序。 在这个应用程序中,我有一个底部导航栏,因此我可以在 4 个片段 A、B、C 和 D 之间进行通信。 我把 A 片段作为默认的主片段。 问题是,如果我想启动我的 navigationBottom 活动(A 是 LogInstrong 文本之后的默认主页片段) 例如,我想以片段 C 开头。

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav"
    app:startDestination="@id/account">

【问题讨论】:

  • 能否分享您的活动代码,以及您实现底部导航的类代码。?

标签: java android android-fragments mobile navigationbar


【解决方案1】:

这可以通过条件导航来实现,我推荐这篇中篇文章:https://medium.com/androiddevelopers/navigation-conditional-navigation-e82d7e4905f0

尤其是围绕此代码 sn-p 的部分(从媒体文章复制粘贴),它显示了如何根据共享首选项值更改默认屏幕:

donutListViewModel.isFirstRun().observe(viewLifecycleOwner) { s ->
    if (s == UserPreferencesRepository.Selection.NOT_SELECTED) {
        val navController = findNavController()
        navController.navigate(
            DonutListDirections.actionDonutListToSelectionFragment()
        )
    }
}

【讨论】:

    【解决方案2】:

    欢迎来到 Stackoverflow :)

    解决方案 在您的第一个活动中添加这些行(您将从该活动移至 BottomNav 类)

    `Intent intent = new Intent(YourActivity.this, BottomNavAvtivity.class);
            intent.putExtra("FromMainActivity", "1");
            startActivity(intent);`
    

    接下来,在您的第二个 Activity(实现 BottomNav 的类)中,在 BottomNavigation 类的 onCreate() 中编写以下代码:

    Intent i = getIntent();
        String data = i.getStringExtra("FromReservation");
    
        if (data != null && data.contentEquals("1")) {
    
          yourBottomNav.setFragment(yourFragment);
    }
    

    【讨论】:

    • 您好,感谢您的解释,但这停止了应用程序的工作
    • bottomNavigationView=(BottomNavigationView)findViewById(R.id.Nav);意图 i = getIntent();字符串数据 = i.getStringExtra("FromMainActivity"); if (data != null && data.contentEquals("1")) { replace(new Help()); //bottomNavigationView.setFragment(R.id.help); }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多