简体   繁体   中英

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

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()
        )
    }
}

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM