安卓:屏幕上的方向变化fragment.getActivity为null [英] Android: on screen orientation change fragment.getActivity is null

查看:198
本文介绍了安卓:屏幕上的方向变化fragment.getActivity为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会尽力解释我的问题:

I'll try to explain my problem:

我所有的片段用setRetainInstance(真)
在我的活动的onCreate我这样做:

all my fragments are using setRetainInstance(true) In my activity onCreate I'm doing this:

    if (savedInstanceState == null) {
        fragment = onCreatePane();
        fragment.setArguments(intentToFragmentArguments(getIntent()));

        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        trans.add(R.id.root_container, fragment, getFragmentTag());
        trans.commit();
    } else {
        FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
        if ( fragment == null ) {

            fragment = getSupportFragmentManager().findFragmentByTag(getFragmentTag());
        }

        if (fragment == null) {
            fragment = onCreatePane();
            fragment.setArguments(intentToFragmentArguments(getIntent()));
        }           

        trans.add(R.id.root_container, fragment, getFragmentTag());
        trans.commit();

    }

所以,当我创建活动,savedInstance为空我创作的片段,我设置它的参数,我开始交易,我的片段添加到交易与它自己的标签(把它找回来以后)。

So when I create the activity and savedInstance is null I create the Fragment, I set it's arguments, I begin the transaction and add my fragment to the transaction with it's own tag (to get it back later).

用户与活动交互,并改变它的方向。该活动被破坏并重新创建(如正常活动lifelycle)。所以现在进入别的,片段为null,并且我做了片段= getSupportFragmentManager()findFragmentByTag(getFragmentTag()); ,它返回fragmentManager holded者正确的片段

The user interact with the activity and change the orientation. The activity is destroyed and recreated (as normal activity lifelycle). So now it enter the else, the fragment is null and I do a fragment = getSupportFragmentManager().findFragmentByTag(getFragmentTag()); that returns the correct Fragment holded by the fragmentManager.

问题是,该片段坚持已destoyed所以,如果我做了fragment.getActivity返回null老活动的参考。如何更新片段参考acitivy到新重新创建的活动?

The problem is that this fragment hold a reference to the old Activity that has been destoyed so if I do a fragment.getActivity it returns null. How can I update the fragment reference to the acitivy to the new re-created Activity?

更新:为了更precise我在调用该onNewIntent的SearchActivity时,它得到一个新的搜索。所以实际的互动是这个 - >用户不要搜索 - >搜索正确显示 - >用户改变方向,显示正确结果(如果用户与互动效果都是很好的) - >用户做从搜索按钮创建一个新的搜索和这个叫SearchActivity的onNewIntent分派新的意图,具有搜索逻辑片段。在这里,它崩溃,因为参考活动是空

UPDATE: To be more precise I'm on the SearchActivity that call the onNewIntent when it get a new Search. So the actual interaction is this-> user do a search -> search is displayed correctly -> user change orientation, result is displayed correctly (if user interact with results they are fine) -> user do a new search from the search button and this call the SearchActivity's onNewIntent that dispatch the new intent to the fragment that has the search logic. Here it crashes because the reference to the activity is null

推荐答案

只是删除​​从别的部分空检查如果(片段== NULL){片段= getSupportFragmentManager(。 .....让对方向的改变创造了新的一个更新的片段......

just remove null check from else part if ( fragment == null ) { fragment = getSupportFragmentManager( ...... let update fragment with new one created on orientation change ......

这篇关于安卓:屏幕上的方向变化fragment.getActivity为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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