BottomNavigationView后退按钮无法正常工作

时间:2017-06-04 19:40:31

标签: java android xml

enter image description here

当按回按钮时,我的片段正在切换到主页,但是底部图标没有改变。我在这里发布了我的所有代码。如果我从底部导航视图项目中选择了两个以上的导航按钮,那么当我按下后退按钮时它将重定向到上次选择的按钮项目。

但现在发生的事情是假设我在最后一项上说'通知'[正如你在屏幕截图中看到的],现在当我按下后退按钮时它会直接将我带到'主页'按钮,但是我想要的是它应该先带我去“搜索”按钮项目,然后再带到'主页'而不是直接到'家'。

我怎样才能实现这个目标?

这是布局:

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

以下是活动:

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


}

请帮助我任何人.....

2 个答案:

答案 0 :(得分:3)

您可以使用template: <h1>{{ project.username }}</h1>选择所需的BottomNavigationView.setSelectedItemId(ITEM_ID)

MenuItem中,使用onBackPressed()mBottomNav.setSelectedItemId(R.id.menu_home)

更新mBottomNav.setSelectedItemId(homeItem.getItemId())方法,如下所示:

onBackPressed()

希望这会有所帮助〜

答案 1 :(得分:0)

如果您想返回堆栈中的所有项目(不仅是首页),请尝试以下代码:

init_var()

然后,在BackPressed上添加以下代码:

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