Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BottomNavigationView Back button not working properly

Tags:

java

android

xml

enter image description here

When is press back button my fragment is changing to home page, but bottom icon is not changing. I posted my all code here. If i select more then two navigation button from bottom navigation view items then when i press back button it'll redirect to last selected button item.

But now what happening is that suppose i'm on the last item say 'Notification' [as you can see in screenshot], now when i press back button it'll directly take me to 'Home' button, but what i want is that it should take me first to 'Search' button item and then to 'Home' not directly to 'home'.

How can i achieve this??

Here is the layout:

 <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                xmlns:design="http://schemas.android.com/apk/res-auto"
                android:id="@+id/activity_main"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                tools:context="com.segunfamisa.sample.bottomnav.MainActivity">

                <FrameLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="1"
                    android:background="#f1f1f1">

                </FrameLayout>

                <android.support.design.widget.BottomNavigationView
                    android:id="@+id/navigation"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="start"
                    design:menu="@menu/bottom_nav_items" />
            </LinearLayout>

Here is the activity:

    public class MainActivity extends AppCompatActivity {
    private static final String SELECTED_ITEM = "arg_selected_item";

    private BottomNavigationView mBottomNav;
    private int mSelectedItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBottomNav = (BottomNavigationView) findViewById(R.id.navigation);
        mBottomNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectFragment(item);
                return true;
            }
        });

        MenuItem selectedItem;
        if (savedInstanceState != null) {
            mSelectedItem = savedInstanceState.getInt(SELECTED_ITEM, 0);
            selectedItem = mBottomNav.getMenu().findItem(mSelectedItem);
        } else
        {
            selectedItem = mBottomNav.getMenu().getItem(0);
        }

        selectFragment(selectedItem);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putInt(SELECTED_ITEM, mSelectedItem);
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onBackPressed() {
        MenuItem homeItem = mBottomNav.getMenu().getItem(0);

        if (mSelectedItem != homeItem.getItemId()) {
            // select home item
           // homeItem.setCheckable(true);

            Log.d("backpressids","****  "+mSelectedItem);
            selectFragment(homeItem);

        } else {

            super.onBackPressed();

        }
    }

    private void selectFragment(MenuItem item) {
        Fragment frag = null;
        item.setCheckable(true);
        // init corresponding fragment
        switch (item.getItemId()) {
            case R.id.menu_home:

                TabFragmentOne fragmentone = new TabFragmentOne();
                FragmentTransaction ft =  getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.container, fragmentone);
                ft.commit();


                break;
            case R.id.menu_notifications:


                TabFragmentThree fragmentone_Three = new TabFragmentThree();
                FragmentTransaction ft_three =  getSupportFragmentManager().beginTransaction();
                ft_three.replace(R.id.container, fragmentone_Three);
                ft_three.commit();

                break;
            case R.id.menu_search:


                TabFragmentTwo tabFragmentTwo = new TabFragmentTwo();
                FragmentTransaction ft_two =  getSupportFragmentManager().beginTransaction();
                ft_two.replace(R.id.container,tabFragmentTwo);
                ft_two.commit();


                break;
        }

        // update selected item
        mSelectedItem = item.getItemId();


        updateToolbarText(item.getTitle());

        if (frag != null) {
            TabFragmentOne fragmentone = new TabFragmentOne();
            FragmentTransaction ft =  getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.container, fragmentone);
            ft.commit();
        }
    }

    private void updateToolbarText(CharSequence text) {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle(text);
        }
    }


}

please help me any one.....

like image 465
Gowthaman M Avatar asked Jun 04 '17 19:06

Gowthaman M


2 Answers

You can use BottomNavigationView.setSelectedItemId(ITEM_ID) to select desired MenuItem:

In onBackPressed(), use mBottomNav.setSelectedItemId(R.id.menu_home) or mBottomNav.setSelectedItemId(homeItem.getItemId()).

Update onBackPressed() method as below:

@Override
public void onBackPressed() {
    MenuItem homeItem = mBottomNav.getMenu().getItem(0);

    if (mSelectedItem != homeItem.getItemId()) {

        selectFragment(homeItem);

        // Select home item
        mBottomNav.setSelectedItemId(homeItem.getItemId());
    } else {
        super.onBackPressed();
    }
}

Hope this will help~

like image 126
Ferdous Ahamed Avatar answered Sep 29 '22 22:09

Ferdous Ahamed


If you want to go back to all the items on the stack (not only home), try the following code:

FragmentTransaction transaction = mFragmentManager.beginTransaction();
transaction.replace(R.id.container, fragment);
if (!isFirstFragment) {
    transaction.addToBackStack(null);
}
transaction.commit();

Then, add this code onBackPressed:

@Override
public void onBackPressed() {
    int count = getSupportFragmentManager().getBackStackEntryCount();
    if (count == 0) {
        super.onBackPressed();
    } else {
        int index = ((getSupportFragmentManager().getBackStackEntryCount()) -1);
        getSupportFragmentManager().popBackStack();
        FragmentManager.BackStackEntry backEntry = getSupportFragmentManager().getBackStackEntryAt(index);
        int stackId = backEntry.getId();
        mBottomNavigationView.getMenu().getItem(stackId).setChecked(true);
    }
}
like image 39
L Kemp Avatar answered Sep 30 '22 00:09

L Kemp