【问题标题】:Move to MainActivity with Jetpack Navigation使用 Jetpack Navigation 移动到 MainActivity
【发布时间】:2019-10-25 12:42:23
【问题描述】:

如何从 MainActivity 中的 SettingsListFragment 移动?

<fragment
        android:id="@+id/settings_list_fragment"
        android:name="com.mandarine.targetList.features.settings.SettingsListFragment"
        android:label="@string/settings"
        tools:layout="@layout/fragment_settings_list">
    </fragment>

因为在我的导航图中我没有 MainActivity 的屏幕。 我的一个活动中的整个导航,你可以检查代码

class MainActivity : AppCompatActivity(), MainActivityViewContract {

    private lateinit var auth: FirebaseAuth
    private lateinit var mAuthStateListener: FirebaseAuth.AuthStateListener
    private val presenter = MainActivityPresenter(contract = this)
    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        signIn()
        setupViews()
    }

    override fun onSupportNavigateUp(): Boolean {
        return findNavController(R.id.nav_host_fragment).navigateUp(appBarConfiguration)
    }

    override fun onResume() {
        super.onResume()
        auth.addAuthStateListener(mAuthStateListener)
    }

    override fun onPause() {
        super.onPause()
        auth.removeAuthStateListener(mAuthStateListener)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        presenter.onActivityResult(requestCode, resultCode)
    }

    override fun cancelSignIn() {
        finish()
    }

    private fun setupViews() {
        val host: NavHostFragment = supportFragmentManager
            .findFragmentById(R.id.nav_host_fragment) as NavHostFragment? ?: return

        val navController = host.navController
        appBarConfiguration = AppBarConfiguration(navController.graph)

        val drawerLayout: DrawerLayout? = findViewById(R.id.drawer_layout)
        appBarConfiguration = AppBarConfiguration(
            setOf(R.id.target_list, R.id.settings_list_fragment, R.id.calendar_fragment),
            drawerLayout
        )
        setupActionBar(navController, appBarConfiguration)
        setupNavigationMenu(navController)
        setupBottomNavMenu(navController)
    }

    private fun setupNavigationMenu(navController: NavController) {
        val sideNavView = findViewById<NavigationView>(R.id.nav_view)
        sideNavView?.setupWithNavController(navController)
    }

    private fun setupActionBar(
        navController: NavController,
        appBarConfig: AppBarConfiguration
    ) {
        setupActionBarWithNavController(navController, appBarConfig)
    }

    private fun setupBottomNavMenu(navController: NavController) {
        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
        bottomNavigationView?.setupWithNavController(navController)
    }

    private fun signIn() {
        auth = FirebaseAuth.getInstance()
        mAuthStateListener = FirebaseAuth.AuthStateListener { firebaseAuth ->
            presenter.signIn(activity = this, user = firebaseAuth.currentUser)
        }
    }
}

【问题讨论】:

  • 你想搬到哪里去?

标签: android kotlin android-jetpack android-jetpack-navigation


【解决方案1】:

您可以在导航图中添加&lt;activity../&gt; 标记并将其设置为操作中的目的地。

<activity
  android:id="@+id/main_activity_destination"
  android:name="com.mandarine.your.package.path.here.MainActivity" />

然后,您可以拨打navController.navigate(R.id.main_activity_destination)

或者,如果您想通过导航图更好地控制流程,您可以创建一个操作:

<action
  android:id="@+id/go_to_main_activity"
  app:clearTask="true"
  app:destination="@+id/main_activity_destination"
  app:launchSingleTop="true"
  app:popUpTo="@id/main_activity_destination" />

然后,您可以拨打navController.navigate(R.id.go_to_main_activity)

【讨论】:

  • 我无法在导航中添加 &lt;activity&gt; 标签因为在项目中我有 1 个活动,而整个逻辑都在 MainActivity 中我还添加了我的活动,现在你可以检查它。
  • 我对您的问题的初步理解是您想从 SettingsListFragment 移至 MainActivity。现在,在您对其进行编辑之后,不清楚您要导航到哪里,因为您已经在 MainActivity 中。请描述您的目的地是哪个屏幕
【解决方案2】:

这个问题并不完全清楚,但看起来你想要实现的是使用 NavController 的片段导航。在回答问题之前,让我先给大家简单了解一下新的喷气背包导航是如何工作的。

您的应用程序中将只有一个活动(也可以有多个活动,但为了解释起见,我坚持使用一个)。在此特定活动中,您只有一个 NavigationHostFragment,它负责您的所有应用导航。这个NavigationHostFragment 将相当于以旧方式添加片段的容器。 NavigationHostFragment 将有一个导航控制器,它将链接到情节提要。您在问题中提到的navigation.xml 文件是您的故事板。

故事板将包含您在导航和导航方向中需要的所有片段。导航方向称为操作。片段中的每个动作都将导航到单独的片段。

因此导航与 Activity 无关,这一切都发生在 NavigationHostFragment(我希望你已经在 Activity 的 xml 文件中)。

有了这些基础知识,有两个片段,其中FragmentA 正在导航到FragmentB,您的导航 xml 文件将看起来像这样:

<fragment android:id="@+id/fragmentA" android:name="com.xxx.xxx.FragmentA"
          android:label="fragment_a" tools:layout="@layout/fragment_a">
    <action android:id="@+id/action_fragmentA_to_fragmentB"
            app:destination="@id/fragmentB"/>
</fragment>
<fragment android:id="@+id/fragmentB"
          android:name="com.xxx.xxx.FragmentB"
          android:label="fragment_b" tools:layout="@layout/fragment_b">
</fragment>

然后在FragmentA 代码中,您需要执行导航,只需调用:

findNavController().navigate(R.id.action_fragmentA_to_fragmentB)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多