0

我是 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

4 回答 4

1

您可以将标签添加到您的片段。这样,您只需检查标签名称。

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

这是如何检查您的片段标签,将此检查放在您的 backpress 方法上的活动或您的closeApp()方法中

function void closeApp(){
    Fragment fragment = getFragmentManager().findFragmentByTag("HomeActivity");
    if (fragment != null) {
        if(fragment.isVisible()){
            HomeActivity.this.finish(); 
        }
    }
}
于 2017-07-10T09:42:58.277 回答
1

在 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;
}
于 2017-07-10T10:22:29.070 回答
0

在您的 MainActivity 类onBackPressed中覆盖并显示对话框,如下所示:

@Override
public void onBackPressed() {
    //show dialog box here 
}

在每个具有以下代码的Fragment实现方法中:onResume()

     @Override
    public void onResume() {
        super.onResume();
        getView().setFocusableInTouchMode(true);
        getView().requestFocus();
        getView().setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
              //homefragment class name     
              Class fragmentClass = HomeFragment.class;

             try {

             Fragment  fragment = (Fragment) fragmentClass.newInstance();
             FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                        fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return true;

                }

                return false;
            }
        });
    }
于 2017-07-10T09:56:21.890 回答
0

为简单起见,只需转到您的HomeFragment.java并编辑方法onBackPressed()以使用finish().
而对于其他人Fragment.java,只需使用popBackStack()功能,您可以在这里搜索: https://developer.android.com/reference/android/app/FragmentManager.html#popBackStack()

于 2017-07-10T09:48:29.250 回答