操作栏检测后退按钮点击的片段 [英] Action Bar detect back button click in fragment

查看:118
本文介绍了操作栏检测后退按钮点击的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个操作栏后退按钮它使一个片段。

code:

  @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);
        setHasOptionsMenu(真正的);
        动作条=((MainActivity)getActivity())getSupportActionBar()。
        actionBar.setDisplayHomeAsUpEnabled(真正的);

        actionBar.setCustomView(R.layout.custom_action_bar);
        actionBar.setDisplayShowTitleEnabled(假);
        actionBar.setDisplayShowCustomEnabled(真正的);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        mInflater =(LayoutInflater)getActivity()getSystemService(Context.LAYOUT_INFLATER_SERVICE)。
        查看查看= mInflater.inflate(R.layout.fragment_layout,集装箱,假);

        返回查看;
    }
 

在上面的code actionBar.setDisplayHomeAsUpEnabled(真); 允许在操作栏上的后退按钮,但怎么才能检测到点击它

我已经研究过很多例子,并试图以下,但仍然没有工作:

在片段:

  @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            Toast.makeText(getActivity(),返回,Toast.LENGTH_LONG).show();
            打破;
        默认:
            打破;
        }
        返回super.onOptionsItemSelected(项目);
    }
 

也被显示在片段中的后退按钮是黑色的,我需要在白色的颜色或自定义颜色我​​怎样才能改变这种状况呢?

解决方案

我面临同样的问题,后来我把这个code活性内部,从碎片里的后退按钮的工作:

  @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){
        案例android.R.id.home:
            Toast.makeText(getActivity(),返回,Toast.LENGTH_LONG).show();
            打破;
        默认:
            打破;
        }
        返回super.onOptionsItemSelected(项目);
    }
 

I have a Fragment with action bar back button enabled in it.

Code :

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        setHasOptionsMenu(true);
        actionBar = ((MainActivity)getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setCustomView(R.layout.custom_action_bar);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.fragment_layout, container, false);

        return view;
    }

In the above code actionBar.setDisplayHomeAsUpEnabled(true); enables the back button in the Action Bar but how can we detect the click on it ?

I have looked into many examples and tried the below but still not working :

In Fragment :

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Toast.makeText(getActivity(), "Back", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

Also the back button which is displayed in the fragment is of Black color i need that in White color or with custom color how can i change that as well ?

解决方案

I was facing the same problem, but then I put this code inside the activity and the back button worked from inside the fragment:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            Toast.makeText(getActivity(), "Back", Toast.LENGTH_LONG).show();
            break;
        default:
            break;
        }
        return super.onOptionsItemSelected(item);
    }

这篇关于操作栏检测后退按钮点击的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆