如何在多个片段中处理onBackPressed()

时间:2017-07-10 09:40:31

标签: android android-fragments drawerlayout android-drawer

我是Android初学者,我正在开发一个应用程序,其中我通过活动使用了多个片段。

private void displaySelectedScreen(int itemId) {
        Fragment fragment = null;
        switch (itemId) {

            case R.id.nav_home:
                fragment = new HomeFragment();
                break;

            case R.id.nav_attendance:
                fragment = new AttendanceFragment();
                break;

            case R.id.nav_notification:
                fragment = new NotificationFragment();
                break;

            case R.id.nav_event:
               fragment = new EventFragment();
                break;

            case R.id.nav_fee:
               fragment = new FeesFragment();
                break;

            case R.id.nav_news:
                fragment = new NewsFragment();
                break;

        }
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.frame_container, fragment);
            ft.addToBackStack(null);
            ft.commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);

    }

如果抽屉已经关闭,请使用onBackPressed()方法关闭应用。现在,如果当前片段为HomeFragment并且当前片段不是HomeFragment,则我想关闭应用,然后由HomeFragment替换。我不知道,怎么做?

public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        closeApp();
    }
}

4 个答案:

答案 0 :(得分:1)

您可以为片段添加标记。这样你只需要检查标签名称。

Fragment fragment = new Fragment();
getFragmentManager().beginTransaction()
    .replace(R.id.FrameLayoutId,fragment,"TAG_NAME")
    .addToBackStack("SOURCE_TAG_NAME").commit(); 

这是检查您的片段代码的方法,在活动上或closeApp()方法

中对您的反印方法进行检查
function void closeApp(){
    Fragment fragment = getFragmentManager().findFragmentByTag("HomeActivity");
    if (fragment != null) {
        if(fragment.isVisible()){
            HomeActivity.this.finish(); 
        }
    }
}

答案 1 :(得分:0)

简单来说,只需转到HomeFragment.java并修改方法onBackPressed()即可使用 finish() 退出应用程序。
对于其他人Fragment.java,只需使用 popBackStack() 功能,您可以在此处搜索: https://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

答案 2 :(得分:0)

在您的MainActivity类中覆盖<p:fileUpload mode="simple" id="recupereFile" value="#{FileUploadView.file}" /> 并显示对话框,如下所示:

onBackPressed

在每个@Override public void onBackPressed() { //show dialog box here } 工具Fragment方法中使用以下代码:

onResume()

答案 3 :(得分:0)

在NavigationActivity中使用此代码onBackPressed()方法。我希望能解决你。

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        Fragment f = getVisibleFragment();// to get which fragment now 
        if (f.getClass() != MainFragment.class) {
            Fragment fragment = null;
            try {
                fragment = (Fragment) MainFragment.class.newInstance();
            } catch (Exception e) {
                e.printStackTrace();
            }

            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.frame, fragment).commit(); // this is your framelayout
        }else {
            super.onBackPressed();
        }

    }
}

getVisibleFragment()函数

public Fragment getVisibleFragment() {
    FragmentManager fragmentManager = NavigationActivity.this.getSupportFragmentManager();
    List<Fragment> fragments = fragmentManager.getFragments();
    if (fragments != null) {
        for (Fragment fragment : fragments) {
            if (fragment != null && fragment.isVisible())
                return fragment;
        }
    }
    return null;
}