停止onCreateOptionsMenu()从分离的片段被称为 [英] Stop onCreateOptionsMenu() to be called from detached Fragment

查看:248
本文介绍了停止onCreateOptionsMenu()从分离的片段被称为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多code,但请阅读第一段并走得更远,如果你需要更多的信息!谢谢!

I know there is LOT of code but please read the first paragraph and go further if you need more info ! Thanks!

我想在我的3个选项卡活动(没有查看传呼机),只是静态的标签显示上下文菜单项。其主要活动是ActionTab.listener。我用ActionBarActivity用行动吧compat的。在code的主要活动如下。我用谷歌文档附加和分离片段(创建,如果他们是空的第一次)。这里是我的LogCat中,你可以看到, onCreateOptionMenu被称为为每个现有的片段,即使只有一个连接并显示为什么?

I want to display contextual menu item in my 3 tabs activity (no view pager), just static tabs. The main activity is the ActionTab.listener. I used ActionBarActivity with action bar compat. The code for the main activity is below. I used the Google docs to attach and detach fragments (that are created if they are null the first time). Here is my LogCat, you can see that onCreateOptionMenu is called for each existing fragment even when only one is attached and displayed Why?

LogCat中:

V / MainActivity(22555):上创建MainActivity的(应用程序启动)

V/MainActivity(22555): on create MainActivity (application starts)

V / onTabSelected(22555):启动

V/onTabSelected(22555): starting

E / MainActivity(22555):FRAG 1为null

E/MainActivity(22555): frag 1 is null

V / onTabSelected(22555):添加片段1的(创建第一个片段)

V/onTabSelected(22555): add fragment 1 (creates 1st fragment)

D /片段1(22555):在连接

D/Fragment1(22555): on Attach

D /片段1(22555):上创建

D/Fragment1(22555): on create

D /片段1(22555): - onCreateOPTIONMenu的(一切都很好)

D/Fragment1(22555): - onCreateOPTIONMenu (all is fine)

D /片段1(22555):上创建视图

D/Fragment1(22555): on create View

D /片段1(22555):在活动创建

D/Fragment1(22555): on Activity Created

D /片段1(22555):在启动

D/Fragment1(22555): on Start

D /片段1(22555):在简历

D/Fragment1(22555): on Resume

D / dalvikvm(22555):GC_CONCURRENT释放159K,4%的自由9115K / 9415K,暂停1毫秒+ 2ms的

D/dalvikvm(22555): GC_CONCURRENT freed 159K, 4% free 9115K/9415K, paused 1ms+2ms

D /剪贴板(22555):隐藏在开始输入剪贴板对话框:由别人完成...

D/CLIPBOARD(22555): Hide Clipboard dialog at Starting input: finished by someone else... !

V /选项卡取消选中(22555):在开始的(我selecte TAB2:回调就取消选择TAB1)

V/on Tab Un-selected(22555): starting (I selecte TAB2 : callback on unselect Tab1)

V /标签上未选1(22555):分离frag1的(FragTransac.detach(frag1)已通过)

V/on Tab 1 Unselected(22555): detach frag1 (FragTransac.detach(frag1) has been passed)

V / onTabSelected(22555):启动

V/onTabSelected(22555): starting

E / MainActivity(22555):FRAG 2是空的(OK创建frag2)

E/MainActivity(22555): frag 2 is null (ok that creates frag2)

V / onTabSelected(22555):添加片段2

V/onTabSelected(22555): add fragment 2

D / Fragment2(22555):在连接

D/Fragment2(22555): on Attach

D /片段1(22555): - onCreateOPTIONMenu的(!为什么叫,你超脱,frag1)

D/Fragment1(22555): - onCreateOPTIONMenu (Why is it called, you are detached, frag1!)

D / Fragment2(22555): - onCreateOPTIONMenu的(frag2 OK)

D/Fragment2(22555): - onCreateOPTIONMenu (frag2 ok)

D / Fragment2(22555):上创建

D/Fragment2(22555): on create

D / Fragment2(22555):上创建视图

D/Fragment2(22555): on create View

D / Fragment2(22555):在活动创建

D/Fragment2(22555): on Activity Created

D / Fragment2(22555):在启动

D/Fragment2(22555): on Start

D / Fragment2(22555):在简历

D/Fragment2(22555): on Resume

V /选项卡取消选中(22555):启动

V/on Tab Un-selected(22555): starting

V /选项卡2未选定(22555):分离frag2

V/on Tab 2 Unselected(22555): detach frag2

V / onTabSelected(22555):启动

V/onTabSelected(22555): starting

E / MainActivity(22555):FRAG 3为null

E/MainActivity(22555): frag 3 is null

V / onTabSelected(22555):添加片段3

V/onTabSelected(22555): add fragment 3

D / Fragment3(22555):在连接

D/Fragment3(22555): on Attach

D /片段1(22555): - onCreateOPTIONMenu的(同样的问题)

D/Fragment1(22555): - onCreateOPTIONMenu (again problem)

D / Fragment2(22555): - onCreateOPTIONMenu的(问题重复frag2)

D/Fragment2(22555): - onCreateOPTIONMenu (problem repeated for frag2)

D / Fragment3(22555): - onCreateOPTIONMenu的(行为我期待frag3)

D/Fragment3(22555): - onCreateOPTIONMenu (behavior I expect for frag3)

D / Fragment3(22555):上创建

D/Fragment3(22555): on create

D / Fragment3(22555):在活动创建

D/Fragment3(22555): on Activity Created

D / Fragment3(22555):在启动

D/Fragment3(22555): on Start

D / Fragment3(22555):在简历

D/Fragment3(22555): on Resume

这是我的主要活动code:主要活动是ActionTab.listener。我用ActionBarActivity用行动吧compat的。在code的主要活动如下。我用谷歌文档附加和分离片段(创建,如果他们是空的第一次)。

This is my main activity code: The main activity is the ActionTab.listener. I used ActionBarActivity with action bar compat. The code for the main activity is below. I used the Google docs to attach and detach fragments (that are created if they are null the first time).

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("MainActivity", "on create MainActivity");
    // Set the Action Bar to use tabs for navigation
    ab = getSupportActionBar();
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    tab1 = ab.newTab().setText("Tab 1").setTabListener(this);
    ab.addTab(tab1);
    tab2 = ab.newTab().setText("Tab 2").setTabListener(this);
    ab.addTab(tab2);
    tab3 = ab.newTab().setText("Tab 3").setTabListener(this);
    ab.addTab(tab3);
}

@Override
public void onResume(){ 
    super.onResume();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate menu from menu resource (res/menu/main)
    getMenuInflater().inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean isInBooking() {
    return isInBooking;
}

// Implemented from ActionBar.TabListener
@Override
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
    // This is called when a tab is selected.
    Log.v("onTabSelected", "starting");
    switch(tab.getPosition()){
    case 0: 

        if (frag1==null) {
            Log.e(TAG, "frag 1 is null");
            frag1 = new Fragment1();
            ft.add(android.R.id.content, frag1);
            Log.v("onTabSelected", "add fragment 1");
        }else {
            ft.attach(frag1);
            Log.v("onTabSelected", "Attach fragment 1");
        }
        break;
    case 1:
        if (frag2==null) {
            Log.e(TAG, "frag 2 is null");
            frag2 = new Fragment2();
            ft.add(android.R.id.content, frag2);
            Log.v("onTabSelected", "add fragment 2");
        } else {
            ft.attach(frag2);
            Log.v("onTabSelected", "Attach fragment 2");
        }
        break;
    case 2:
        if (frag3==null) {
            Log.e(TAG, "frag 3 is null");
            frag3 = new Fragment3();
            ft.add(android.R.id.content, frag3);
            Log.v("onTabSelected", "add fragment 3");
        } else {
            ft.attach(frag3);
            Log.v("onTabSelected", "Attach fragment 3");
        }
        break;
    default:
        break;
    }
}

// Implemented from ActionBar.TabListener
@Override
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
    // This is called when a previously selected tab is unselected.
    Log.v("on Tab  Un-selected", "starting");
    switch(tab.getPosition()){
    case 0: 
        ft.detach(frag1);
        Log.v("on Tab 1 Unselected", "detach frag1");
        break;
    case 1:
        ft.detach(frag2);
        Log.v("on Tab 2 Unselected", "detach frag2");
        break;
    case 2:
        ft.detach( frag3);
        Log.v("on Tab 3 Unselected", "detach frag3");
        break;
    default:
        break;
    }
}

该片段被构建为延伸片段。 这里是片段onCreateOptionsMenu()。 我把setHasOption为true。也许我应该包梁这样的一个控制,因为我看到了CommonsWare例如<一href="https://github.com/commonsguy/cw-omnibus/blob/master/ViewPager/ActionBar/src/com/commonsware/android/pagerbar/EditorFragment.java"相对=nofollow>此处,但我不明白,为什么我应该怎么办呢?

The fragments are built as extension of Fragment. Here is the fragment onCreateOptionsMenu(). I put setHasOption to true. Maybe I should wraped this in a control as I saw on the CommonsWare example here but I don't understand how and why should I do that?

@Override 
public void onCreateOptionsMenu(android.view.Menu menu, android.view.MenuInflater inflater) {
    if(!isDetached()) {
        Log.d(TAG, " - onCreateOPTIONMenu");        
        inflater.inflate(R.menu.fragment1, menu);
    } else Log.v(TAG, "not attached - onCreateOPTIONMenu"); 
} 

我也试过 isAdded() isHidden 有没有好结果(总是隐藏,总是添加)。但我有电话分离和附加,什么是怎么回事?

I also tried isAdded() or isHidden with no good results (always hidden, always added). But I do have calls detach and attach, What is going on here?

推荐答案

这是与这样的解释:的https://groups.google.com/forum/#!msg/android-platform/QlkLMsncDwg/u3iZ-q7FCVkJ

在最后我离开了那个简单的实现要以期寻呼机的功能更强大,更精致,我觉得。它原生管理正确的菜单,并提供了漂亮的动画过渡。

In the end I left that simple implementation to go with a view pager that is more powerful and more refined, I feel. It natively manages correctly the menu and provides nice animation transition.

这篇关于停止onCreateOptionsMenu()从分离的片段被称为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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