Android JetPack Navigation : How to open a fragment from an activity not associated with it?

岁酱吖の 提交于 2019-12-25 01:06:46

问题


scenario :

MainActivity.kt : activity_main.xml contains my_nav_host_fragment

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.

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

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.

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

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

Please tell how to do it using JetPack Navigation components.

Note: Principles of navigation say that only one activity must exist but here I am compelled to use an Activity for GoogleSignIn due to Firebase restrictions.


回答1:


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



来源:https://stackoverflow.com/questions/53854226/android-jetpack-navigation-how-to-open-a-fragment-from-an-activity-not-associa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!