Android JetPack导航:如何从与其不相关的活动中打开片段? [英] Android JetPack Navigation : How to open a fragment from an activity not associated with it?

本文介绍了Android JetPack导航:如何从与其不相关的活动中打开片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景:

MainActivity.kt :activity_main.xml包含my_nav_host_fragment

first_frag_dest 是startDestination,并且还有 second_frag_dest ,分别具有类FirstFragment.ktSecondFragment.kt.

in mobile_navigation.xml first_frag_dest is the startDestination and there is second_frag_dest also, with respective classes FirstFragment.kt and SecondFragment.kt.

我已经设置了底部导航,可以使用Android Jetpack导航组件在两个片段之间进行切换.

I have setup bottom navigation for switch between two fragments using the Android Jetpack Navigation components.

如果FirbaseUser为null,则第一次打开第二个片段时,将启动一个为GoogleSignInusing Firebase配置的欢迎活动".

When the Second Fragment is opened for the first time if FirbaseUser is null, A Welcome Activity is launched which is configured for GoogleSignInusing Firebase.

WelcomeActivity.java中,我有一个函数updateUI(user: FirebaseUser)来更新UI.

In WelcomeActivity.java I have a function updateUI(user: FirebaseUser) to update the UI.

现在,我应该在updateUI函数中编写什么代码,以便将我重定向回第二个片段.

Now, what code should I write in updateUI function such that I am redirected back to the Second Fragment.

请说明如何使用JetPack导航组件进行操作.

Please tell how to do it using JetPack Navigation components.

注意:导航原理说只有一个活动必须存在,但是由于Firebase的限制,在这里我不得不将一个活动用于GoogleSignIn.

推荐答案

如果要关闭WelcomeActivity并返回到其下方的活动,请使用updateUI方法调用

If you want to close your WelcomeActivity and return to the activity below it, have your updateUI method call finish():

活动结束后应调用此按钮

Call this when your activity is done and should be closed

这篇关于Android JetPack导航:如何从与其不相关的活动中打开片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆