启用动作条按钮选项卡中,每次出现的导航。 [英] Enable an ActionBar Button each time navigation within tabs occurs.

查看:237
本文介绍了启用动作条按钮选项卡中,每次出现的导航。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在,我用三个片段指定选项卡的主要活动。我对动作条一个按钮,导航到不同的片段说:关于应用信息一旦用户导航到这个特殊的片段(信息)我禁用它,这样它不会一次又一次地叫。然后在主活动我返回键重新启用它。到现在为止还挺好。但我不能够重新启用一个场景:当用户浏览到的信息片段,并且不preSS回来,但但如果他浏览到不同的标签,信息按钮仍然因为后台禁止说preSS没有被调用。我尝试了很多东西,在onStart()和片段的onResume(),但我不能引用菜单项中的任何这些,因为我得到一个空指针。

code参考:(MainActivity同时呼吁从onOptionsSelected的信息片段):

 公共布尔onOptionsItemSelected(菜单项项){        mMenuItem =项目;
        开关(item.getItemId()){
        案例R.id.info:
            标签D = getActionBar()getSelectedTab()。            的System.out.println(+ d.getText()的toString());
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();            字符串= d.getText()的toString()。
            如果(a.equalsIgnoreCase(提醒)){
                FragmentContact fragmentcontact =新FragmentContact();
                fragmentTransaction.replace(R.id.realtabcontent,fragmentcontact);
                mMenuItem.setEnabled(假);
                //mMenuItem.setIcon(R.drawable.btn_age_01);
            }
            否则,如果(a.equalsIgnoreCase(通知)){
                FragmentContact fragmentcontact =新FragmentContact();
                fragmentTransaction.replace(R.id.realtabcontent2,fragmentcontact);
                mMenuItem.setEnabled(假);            }
            否则,如果(a.equalsIgnoreCase(联系人)){
                FragmentContact fragmentcontact =新FragmentContact();
                fragmentTransaction.replace(R.id.realtabcontent3,fragmentcontact);
                mMenuItem.setEnabled(假);            }
            fragmentTransaction.addToBackStack(NULL);
            fragmentTransaction.commit();            打破;

这是返回键(主要活动):

  @覆盖
    公共无效onBack pressed(){
        mMenuItem.setEnabled(真);
        super.onBack pressed();
    }


解决方案

解决的办法很简单,设置各个碎片的选项菜单使用:

  setHasOptionsMenu(真);

干杯!!

I have a main activity in which I designate tabs using three fragments. I have a button on the ActionBar which navigates to a different fragment say "Info about the app" Once user navigates to this particular fragment (Info) I disable it so that it is not called again and again. Then on the back key in the main activity I re-enable it. So far so good. But I am not able to re-enable it for one scenario: Say if user navigates to the info fragment and does not press back, but however if he navigates to a different tab, the info button is still disabled because back-press has not been called. I tried a lot of things in onStart() and onResume() of fragments but I am not able to reference the menuItem in any of those as I get a null pointer.

Code Reference: (MainActivity while calling the info fragment from onOptionsSelected):

public boolean onOptionsItemSelected(MenuItem item) {

        mMenuItem = item; 
        switch (item.getItemId()) {
        case R.id.info:
            Tab d = getActionBar().getSelectedTab();

            System.out.println(""+d.getText().toString()); 
            FragmentManager fragmentManager = getFragmentManager();  
            FragmentTransaction fragmentTransaction = fragmentManager  
                    .beginTransaction();  

            String a = d.getText().toString(); 
            if(a.equalsIgnoreCase("Reminders")){ 
                FragmentContact fragmentcontact = new FragmentContact(); 
                fragmentTransaction.replace(R.id.realtabcontent, fragmentcontact);  
                mMenuItem.setEnabled(false); 
                //mMenuItem.setIcon(R.drawable.btn_age_01); 
            }
            else if(a.equalsIgnoreCase("Notifications")){
                FragmentContact fragmentcontact = new FragmentContact();
                fragmentTransaction.replace(R.id.realtabcontent2, fragmentcontact);  
                mMenuItem.setEnabled(false); 

            }
            else if(a.equalsIgnoreCase("Contacts")){
                FragmentContact fragmentcontact = new FragmentContact(); 
                fragmentTransaction.replace(R.id.realtabcontent3, fragmentcontact); 
                mMenuItem.setEnabled(false); 

            }
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();  



            break;

on Back key(Main Activity):

@Override
    public void onBackPressed() {
        mMenuItem.setEnabled(true);
        super.onBackPressed();
    }

解决方案

The solution was very simple, to set an options menu for individual "Fragments" use:

setHasOptionsMenu(true);

Cheers!!

这篇关于启用动作条按钮选项卡中,每次出现的导航。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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