如何禁用片段中的抽屉并返回到正确的片段中[英] How to Disable drawer in fragment and back back to correct fragment

本文是小编为大家收集整理的关于如何禁用片段中的抽屉并返回到正确的片段中的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个带有片段布局的主要活动.抽屉有3个选项:

Fragment[1], Fragment[2], Fragment[3].

内部片段[2]和片段[3]是一个按钮.此按钮打开其他片段.片段[4].

我想要片段[4],没有抽屉,但带有一个后退按钮.

这是片段[2]

中的ONCLICK代码
Fragment fragment = new InstalacionesEncontradasFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("key", this.instalacionesConCategorias);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.replace(R.id.main_frame_container, fragment, "ACTIVIDADES").commit();

和片段[4]

ongreate方法:

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

但是该解决方案不起作用.

如何禁用抽屉?我应该在哪里实现返回按钮?在片段[2]或片段[3]?

推荐答案

  1. 您可以使用:

    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    

    这将锁定swipe>

  2. 的抽屉打开
  3. 添加行

    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    

在活动中,使所有片段等片段1、2,3和4等片段可能是在您的情况下,片段4来自不同的片段活动.

本文地址:https://www.itbaoku.cn/post/2091040.html

问题描述

I have a main Activity with a Fragment layout. The drawer has 3 option:

Fragment[1], Fragment[2], Fragment[3].

Inside Fragment[2] and Fragment[3] is one button. This button open other fragment. Fragment[4].

I want Fragment[4] without drawer but with a back button.

This is the onClick code in Fragment[2]

Fragment fragment = new InstalacionesEncontradasFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("key", this.instalacionesConCategorias);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();

FragmentTransaction mFragmentTransaction = fragmentManager.beginTransaction();
mFragmentTransaction.addToBackStack(null);
mFragmentTransaction.replace(R.id.main_frame_container, fragment, "ACTIVIDADES").commit();

And in Fragment[4]

onCreate method:

getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);

But this solution doesn't work.

How to disable the drawer? Where should I implement the back button? In Fragment[2] or Fragment[3]?

推荐答案

  1. You can use :

    mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    

    This will lock drawer opening on swipe

  2. Add the line

    getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
    

in Activity which makes all fragments like Fragment 1, 2,3 and 4. May be in your case, Fragment 4 is from differenct activity than Fragment 2. So, back button press is not working