Med Oussema Zaier
Med Oussema Zaier

Reputation: 51

How to return from activity to a specific fragment in navigation bottom

hello guys, I'm developing an android app in which I need your help. in this app, I have a bottom nav bar so I can communicate between 4 fragments A, B, C, and D. I put the A fragment as the default home fragment. the problem that, if I want to start my navigationBottom activity (A is the default home fragment after LogInstrong text) I want to start it with a fragment C for example.

<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">

Upvotes: 1

Views: 1054

Answers (2)

ahmednawazCS
ahmednawazCS

Reputation: 124

Welcome to Stackoverflow :)

Solution Add these lines in your First Activity(From which you are moving to BottomNav Class)

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

Next,In your Second Activity(Class implementing BottomNav) write the below code in onCreate() of BottomNavigation class:

Intent i = getIntent();
    String data = i.getStringExtra("FromReservation");

    if (data != null && data.contentEquals("1")) {

      yourBottomNav.setFragment(yourFragment);
}

Upvotes: 1

Wang
Wang

Reputation: 1038

This can be achieved with conditional navigation, I would recommend this medium article: https://medium.com/androiddevelopers/navigation-conditional-navigation-e82d7e4905f0

Especially the section around this code snippet (copy-pasted from the medium article) which shows how to change the default screen depending on a shared preferences value:

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

Upvotes: 0

Related Questions