Search code examples
androidandroid-fragmentsandroid-architecture-componentsandroid-jetpackandroid-architecture-navigation

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


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.


Solution

  • 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