On Back按下遍历最后两个选项卡在BottomNavigationView中无法正常工作?

时间:2017-06-05 06:20:17

标签: java android xml android-linearlayout

单击后退按钮,导航到上一个打开的选项卡。您将仅通过遍历已打开的最后两个选项卡退出应用程序。实现它

我只想遍历我打开的所有标签,然后应该退出

这是布局:

 <?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()) {

            selectFragment(homeItem);
            // Select home item
            mBottomNav.setSelectedItemId(homeItem.getItemId());

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

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

1 个答案:

答案 0 :(得分:1)

更新onBackPressed(),如下所示:

@Override
public void onBackPressed() {

    if (mSelectedItem == R.id.menu_notifications) {

        MenuItem searchItem = mBottomNav.getMenu().getItem(1);
        selectFragment(searchItem);
        mBottomNav.setSelectedItemId(searchItem.getItemId());

    } else if (mSelectedItem == R.id.menu_search) {

        MenuItem homeItem = mBottomNav.getMenu().getItem(0);
        selectFragment(homeItem);
        mBottomNav.setSelectedItemId(homeItem.getItemId());

    } else {
        super.onBackPressed();
    }
}

<强>更新

在按下transaction时,尝试将所有片段back stack添加到popback

试试这个:

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

        FragmentManager fragmentManager = getSupportFragmentManager();

        if (fragmentManager.getBackStackEntryCount() > 1) {
            fragmentManager.popBackStackImmediate();
        } else  {
            super.onBackPressed();
        }
    }

    private void selectFragment(MenuItem item) {

        Fragment frag = null;
        item.setCheckable(true);
        switch (item.getItemId()) {
            case R.id.menu_home:

                TabFragmentOne fragmentone = new TabFragmentOne();
                FragmentTransaction ft =  getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.container, fragmentone);
                ft.addToBackStack(null);
                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.addToBackStack(null);
                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.addToBackStack(null);
                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);
        }
    }
}