片段何时可​​见的侦听器 [英] Listener for when a Fragment becomes visible

查看:96
本文介绍了片段何时可​​见的侦听器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很疯狂,试图获得一个optionsMenu为不同的视图提供不同的选项。如果在片段上调用了onResume(),我可以使它工作,但事实并非如此。

I'm going crazy trying to get an optionsMenu to have different options for different views. I could have it working if onResume() was called on my fragments, but it isn't.

我有一个SherlockFragmentActivity,它在onCreate期间添加了这样的SherlockFragment:

I have one SherlockFragmentActivity which during onCreate adds a SherlockFragment like this:

if (getSupportFragmentManager().findFragmentById(android.R.id.content) == null) {
            Fragment f = LoginFragment.newInstance();

            getSupportFragmentManager()
                    .beginTransaction()
                    .add(android.R.id.content, f, "loginfragment")
                    .attach(f)
                    .commit();
        }

这最终会使用相同的方法在其顶部添加另一个SherlockFragment:

This eventually adds another SherlockFragment on top of it using the same method:

Fragment f = OfferListFragment.newInstance();
        getActivity().getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, f, "offerList")
                .addToBackStack(f.getClass().getSimpleName())
                .commit();

最后,这将使用以下方法在顶部启动另一个SherlockFragment:

and finally this launches yet another SherlockFragment on top using the method:

AddOfferFragment newFragment = AddOfferFragment.newInstance(offer);

        getActivity()
                .getSupportFragmentManager()
                .beginTransaction()
                .add(android.R.id.content, newFragment, "addofferdialog")
                .addToBackStack(newFragment.getClass().getSimpleName())
                .commit();

我已经添加了onResume()事件,例如:

I've added onResume() events like so:

@Override
    public void onResume() {
        Log.e(TAG, "onResume");
        super.onResume();
    }

现在我在创建恢复日志时看到,但是当我从其中任何一个按下 Back 时,我根本看不到 onResume`日志。我的选择之一是注销,退出所有这样的视图:

now I see onResume logs when they are created, but when I pressBackfrom any of them, I don't get theonResume` log's at all. 1 of my options is to logout which dismisses all views like this:

FragmentManager fm = getSupportFragmentManager();

        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack(fm.getBackStackEntryAt(0).getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }

但我什至在第一个片段上都没有onResume。

but I don't even get onResume on the very first fragment.

由于另一个问题,片段是透明的此处(我不确定这可能是正常行为)。
我想知道添加片段的方式是否错误?

Due to another issue I have with the fragments being transparent here (This may be normal behaviour, i'm not sure). I'm wondering if the way I am adding the fragments is wrong?

如果没有,那么可能还有什么问题?

If not, what else could be the problem?

推荐答案

正如JonasSeputis和OP自己所指出的,解决方案是更改 transaction.add() transaction.replace()

As noted by JonasSeputis and the OP himself, the solution was to change the transaction.add() to transaction.replace()

详细解决方案在这里。

这篇关于片段何时可​​见的侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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