简体   繁体   English

按下返回时的Android底部导航栏转到上一个片段

[英]Android bottom navigation bar when press back go to previous fragment

Here is my navigation bar :这是我的导航栏:

导航栏截图

There are 4 items in my bottom nav bar , I want it to back to previous fragment when I press the back button,我的底部导航栏中有 4 个项目,当我按下后退按钮时,我希望它回到上一个片段,

Here`s my xml :这是我的 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>

Here`s the code for MainActivity :这是 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;
        }
    });

}

I also tried defining a back button with the statements below but they didn`t work :我还尝试使用以下语句定义后退按钮,但它们不起作用:

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

and also "popbackstack()" and "popbackstackImmediate()" did not work!而且“popbackstack()”和“popbackstackImmediate()”也不起作用!

If you want to handle backpress in fragment than you should add addToBackStack() when adding or replacing a fragment如果你想在片段中处理反压,那么你应该在添加或替换片段时添加addToBackStack()

try to add like that in your all fragment尝试在您的所有片段中添加这样的内容

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

It will be handle by default.它将默认处理。

You can also handle in onBackPress() in activity if you want如果需要,您还可以在活动中处理onBackPress()

Following code will set the icon of the home fragment icon of the bottom navigation to active, use accordingly to make active each icon on back press:以下代码将底部导航的主页片段图标的图标设置为活动,相应地使用使每个图标在后退时处于活动状态:

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

Use AlertDialog On BackPressed try this,在 BackPressed 上使用AlertDialog试试这个,

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

I hope this may help you..我希望这可以帮助你..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按“后退”按钮时片段无法返回到先前的状态 - fragment can not go back to previous state when press Back button 当按返回按钮不更新FragmentAdapter时转到上一个片段 - Go to previous fragment when press back button doesnt update FragmentAdapter 按下按钮如何返回上一个片段 - How to go back to the previous Fragment on Press of a button go 按返回时返回第一个片段 - go back to first fragment when press back 向后导航到先前片段时Android片段过渡 - Android fragment transition on back navigation to previous fragment Android:在背面按下返回上一个片段 - Android : Return to previous fragment on back press 返回到android中的上一个片段或主页 - Back press to the previous fragment or home in android 需要按两次返回上一个片段 - Need to press back twice to go back to previous fragment 为什么Android Navigation Component 屏幕不返回上一个Fragment,而是调用了previos Fragment 的onViewCreated 中的一个方法? - Why Android Navigation Component screen not go back to previous Fragment,but a method in onViewCreated of previos Fragment being called? 当我们 go 使用 android 中的 backstack 回到上一个片段时,上一个片段正在重新启动 - Previous fragment is restarting when we go back to previous fragment using backstack in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM