本文介绍了如何实现android工具栏后退按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用自定义工具栏.我需要添加返回按钮.现在,我正在使用此代码添加后退按钮.

I am using a custom toolbar. I need to add back button to it. Now I am using this code to add the back button.

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.back_arrow));
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

这很好.我可以看到添加了后退"按钮.但是考虑一下我在没有后退按钮的Fragment1中的情况.现在,我移至Fragment2并添加后退"按钮.从片段2打开片段3,然后再次添加后退按钮.

This works fine. I can see the back button added. But consider the case where I am in Fragment1 which has no back button. Now I move to Fragment2 and I add in Back Button. From Fragment 2 I open Fragment 3 and I add the back button again.

现在,当我按fragment3中的后退"按钮返回fragment2时,我必须检查"Fragment Stack"以查看是否在fragment 2中需要后退按钮.

Now when I press back button from fragment3 to go back to fragment2 i have to check the Fragment Stack to see whether the back button is required in fragment 2 or not.

当我们将片段推入堆栈时,还有其他方法可以自动处理后退按钮吗?

Is there any other way to handle back button automatically as we push fragments to stack?

推荐答案

您可以轻松处理后退图标.如果您所有的片段都在一个Activity中,那么我真的建议您通过以下方式进行处理:

You can handle back icon very easily. If all of your fragment are in single Activity I really recommend to handle this with following way :

首先创建一个实现FragmentManager .OnBackStackChangedListener的抽象BaseFragment类,然后在其中放入以下方法:

first crate a abstract BaseFragment class which implement FragmentManager .OnBackStackChangedListener then put following method inside that :

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainActivity = (MainActivity) getActivity();

        getFragmentManager().addOnBackStackChangedListener(this);

        shouldDisplayHomeUp();
    }

    @Override
    public void onBackStackChanged() {
        shouldDisplayHomeUp();
    }


    public boolean shouldDisplayHomeUp() {
        //Enable Up button only  if there are entries in the back stack
        boolean canBack = false;
        try {
            canBack = getFragmentManager().getBackStackEntryCount() > 0;
        } catch (Exception ex) {
//            Log.e(getClass().getCanonicalName(), ex.getMessage());getMessage
        }

        if (canBack) {
            mainActivity.drawerDisable();
        } else {
            mainActivity.drawerEnable();
        }
        return canBack;
    }

通过这种方式disableDrawer& enableDrawer函数处理您的Icon,而OnBackPressed方法处理您的BackStack Now,如果需要,当您按back-icon display时,将在您的活动中处理它.您的onBackPressed应该是这样的:

By this way disableDrawer & enableDrawer function handle your Icon and OnBackPressed method handle your BackStack Now in your activity when you press back-icon display if needed. your onBackPressed should be something like this :

 int backStackCount = getSupportFragmentManager().getBackStackEntryCount();

            if (backStackCount == 0) {
                //nothing exist in backStack OS handle it
                super.onBackPressed();
            } else {

                getSupportFragmentManager().popBackStack();
}

查看完整的实现此处.

这篇关于如何实现android工具栏后退按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 21:26