java Android - 如何结束一个片段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17223351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:21:25  来源:igfitidea点击:

Android - How to end a Fragment

javaandroidandroid-fragmentsonpauseactivity-finish

提问by user222786

In my application I have a navigation drawer, and the way my app works is there is only one activity and if you select something from the Navigation drawer it starts/replaces the current fragment. How Can I make it so that when something is selected from the navigation drawer and a new fragment starts the old fragment ends

在我的应用程序中,我有一个导航抽屉,我的应用程序的工作方式是只有一个活动,如果您从导航抽屉中选择某些内容,它会启动/替换当前片段。我如何才能做到当从导航抽屉中选择某些内容并且新片段开始时旧片段结束

I cant do

我做不到

@Override
public void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    getActivity().finish();
}   

because that would kill the app since there is only one activity.

因为这会杀死应用程序,因为只有一项活动。

How do I kill the fragment?

我如何杀死片段?

EDIT: my fragment transaction

编辑:我的片段交易

Home HomeFragment = new Home();
        transaction.replace(R.id.llhome, HomeFragment);
        transaction.addToBackStack(null);
        transaction.commit();
              mDrawerList.setItemChecked(position, true);
                mDrawerLayout.closeDrawer(mDrawerList);

thanks

谢谢

回答by Chetan

Just call:

只需致电:

getActivity().onBackPressed();

回答by CommonsWare

If you are using dynamic fragments, set up via a FragmentTransaction, you could run a transaction to replace()the old fragment with the new one.

如果您使用的是通过 设置的动态片段,则FragmentTransaction可以replace()使用新片段对旧片段运行事务。