Go back to the previous Fragment by clicking the Back Phone button?

Asked

Viewed 217 times

2

I’m trying to implement the functionality of going back and appearing to me the fragment in which I was previously.

That is: fragment1, Fragment 2, Fragment 3 and Fragment 4.

I can go from Fragment 1 to 2, 3 and 4 and so on, since, I have a bottomNavigation. What happens is that when I click the back button on the phone the app turns off and I can’t get back to the previous Fragment...

I’ve seen countless codes, but none of them could help me.

This is part of my MainActivity of BottomNavigation:

    BottomNavigationView navigation = findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    navigation.setSelectedItemId(R.id.navigation_user);
}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_user:
                    Navigation.findNavController(MainActivity.this, R.id.navHostFragment).navigate(R.id.user_dest);
                    activeFragment = new UserFragment();
                    return true;
                case R.id.navigation_maps:
                    Navigation.findNavController(MainActivity.this, R.id.navHostFragment).navigate(R.id.maps_dest, bundle);
                    activeFragment = new MapsFragment();
                    return true;
                case R.id.navigation_history:
                    Navigation.findNavController(MainActivity.this, R.id.navHostFragment).navigate(R.id.resume_dest);
                    activeFragment = new HistoryFragment();
                    return true;

            }
            return false;
        }
};

A code I saw (most of the codes used the FragmentManager):

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = 
fragmentManager.beginTransaction();
fragmentTransaction.replace(..............);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit(); 

I don’t know where to put these pieces of code or what parameters to put on fragmentTransaction.replace().

Some kind soul to help me?

  • I tried to do it here, but I did not succeed, in case I can please post the answer, it is something I have been trying for a long time, the most I can is to return the Fragment to the first Fragment and then in the promising back button, close the app, I will ask a similar question in the stack in English, if you have answer, put here. Abç

  • If you are interested I have made this example: https://github.com/MurilloComino/BottomNavigationViewJetpack

2 answers

1

In fact the user should use the:

getFragmentManager(). popBackStack()

Sort of like this:

@Override
public void onBackPressed(){
    FragmentManager fm = getFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        Log.i("MainActivity", "popping backstack");
        fm.popBackStack();
    } else {
        Log.i("MainActivity", "nothing on backstack, calling super");
        super.onBackPressed();  
    }
}

More information are here: https://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

  • Indeed! I forgot to comment on that detail. Now the answer is complete!!!

0

This method makes have the event button back from mobile:

@Override
    public void onBackPressed() {
        super.onBackPressed();
    }

Use it to call the previous Fragment.

  • The event is used to intercept the click on the back button, correct, but this is not only the user’s question. He wants that at the click of the button the app navigates back through the previous fragments, those that are in backstack.

  • 1

    This answer is not wrong, but there is a complement missing there... I will answer!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.