按下Android后底部导航栏转到上一个片段

时间:2017-08-09 05:51:29

标签: android fragment material

这是我的导航栏:

Screenshot of navigation bar

我的底部导航栏中有4个项目,我希望它在按下后退按钮时返回上一个片段,

这是我的xml:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomnav"
    android:layout_width="382dp"
    android:layout_height="52dp"
    android:layout_marginBottom="0dp"
    app:itemBackground="@color/backbeyez"
    app:itemIconTint="@drawable/nav_items_color"
    app:itemTextColor="@drawable/nav_items_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation"
    tools:layout_editor_absoluteY="515dp">

</android.support.design.widget.BottomNavigationView>

这是MainActivity的代码:

public class MainActivity extends AppCompatActivity {

public void switchorders() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new OrdersLayout()).commit();
}

public void switchcredits() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new CreditLayout()).commit();
}

public void switchworks() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new ListLayout()).commit();
}

public void switchprofile() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new ProfileLayout()).commit();
}

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


    BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottomnav);
    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);


    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {
                case R.id.navigation_orders:
                    switchorders();
                    break;

                case R.id.navigation_credit:
                    switchcredits();
                    break;

                case R.id.navigation_works:
                    switchworks();
                    break;

                case R.id.navigation_profile:
                    switchprofile();
                    break;
            }
            return true;
        }
    });

}

我也尝试使用下面的语句定义一个后退按钮,但它们不起作用:

public void aboutback(View view) {
    if (getSupportFragmentManager().getBackStackEntryCount() > 0)
    {
        boolean done = getSupportFragmentManager().popBackStackImmediate();
    }
}

并且“popbackstack()”和“popbackstackImmediate()”也不起作用!

谢谢大家的帮忙!

3 个答案:

答案 0 :(得分:0)

如果你想在片段中处理背压,你应该在添加或替换片段时添加 addToBackStack()

尝试在你的所有片段中添加这样的内容

public void switchorders() {
    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction().replace(R.id.container, new OrdersLayout()).addToBackStack("TAG").commit();
}

默认情况下会处理。

如果需要,您还可以在活动中处理 onBackPress()

答案 1 :(得分:0)

以下代码将底部导航的主页片段图标的图标设置为活动,并相应地用于使每个图标在按下时均处于活动状态:

final BottomNavigationView mBottomNav = findViewById(R.id.nav_view);
MenuItem homeItem = mBottomNav.getMenu().getItem(0);
mBottomNav.setSelectedItemId(homeItem.getItemId());

答案 2 :(得分:-1)

使用AlertDialog On BackPressed尝试此操作,

@Override
public void onBackPressed() {

    new AlertDialog.Builder(this)
    .setTitle("Really Exit?")
    .setMessage("Are you sure you want to exit?")
    .setNegativeButton(android.R.string.no, null)
    .setPositiveButton(android.R.string.yes, new OnClickListener() {

        public void onClick(DialogInterface arg0, int arg1) {
            WelcomeActivity.super.onBackPressed();
        }
    }).create().show();
}

我希望这对你有帮助..