简体   繁体   English

在backPressed上返回相同的片段

[英]Return to same Fragment on backPressed

I have implemented a ´NavigationView´ which has a few menu options that when user selects one, it changes the "main fragment" view to anothers... My problem is, I'd like that when the user presses the back button, unless he is on the "main fragment" the screen returns to the "main fragment", otherwise close the app (regular onBackPressed() behaviour when there is no activity or backStack). 我实现了一个“ NavigationView”,它具有一些菜单选项,当用户选择一个菜单选项时,它将“主片段”视图更改为另一个...我的问题是,我希望当用户按下后退按钮时,除非他在“主片段”上,屏幕返回“主片段”,否则关闭应用程序(在没有活动或backStack的情况下,常规的onBackPressed()行为)。 What I've done so far: 到目前为止,我所做的是:

@Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {

        switch (menuItem.getItemId()) {

            case R.id.debitsNavId:
                getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
                break;

            case R.id.friendsNavId:
                getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new FriendFragment(), "friend_fragment").commit();
                break;

            case R.id.notificationsNavId:
                getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new RequestFragment(), "request_fragment").commit();
                break;

            case R.id.configurationsNavId:
                Intent i = new Intent(MainActivity.this,
                        SettingsActivity.class);
                startActivity(i);
                break;

            case R.id.logoutNavId:
                logout();
                break;
            default:
                return false;
        }

        menuItem.setChecked(true);
        drawer_layout.closeDrawers();

        return false;

    }

The problem is, if the user navigates from 'FriendFragment' to 'RequestFragment' or vice-versa, the fragment that gets "backstacked" is the previous one (either FriendFragment or RequestFragment) and I'd like to still be "MainFragment". 问题是,如果用户从“ FriendFragment”导航到“ RequestFragment”,反之亦然,则被“反向堆叠”的片段是前一个(FriendFragment或RequestFragment),而我仍然希望是“ MainFragment”。

How can I achieve that ? 我该如何实现?

Hope I made myself clear and thanks for your time ! 希望我能说清楚,并感谢您的宝贵时间!

So basically what you would need to do is that when the MainFrag is visible set a boolean to true and if you navigate away set it to false. 因此,基本上,您需要做的是,当MainFrag可见时,将布尔值设置为true,如果导航离开,将其设置为false。

Then override the onBackPressed, in your main activity, like the following method: 然后在您的主要活动中覆盖onBackPressed,类似于以下方法:

@Override
public void onBackPressed() {
    if (!isMainFragVisible) {
        getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
    }
}

You can set the isMainFragVisible to true in this method: 您可以通过以下方法将isMainFragVisible设置为true:

        case R.id.debitsNavId:
            getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();
isMainFragVisible = true
            break;

And set it to false in any other navigation method. 并在任何其他导航方法中将其设置为false。

这样做

getSupportFragmentManager().beginTransaction().addToBackStack(fragment.getClass().getSimpleName()).replace(R.id.main_fragment_container, new MainFragment(), "main_fragment").commit();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM