方向改变后,FIndFragmentByTag失败

时间:2015-06-19 04:42:52

标签: android android-fragments

我尝试使用UIless保留的Fragment在定位更改期间不丢失服务粘合剂。问题是,在活动onCreate中,findFragmentByTag始终返回null。

这是我的活动:

public abstract class AbstractLearnActivity extends Activity implements FragmentCallbacks {
    private final static String BINDER_FRAG_TAG = "AbstractLearnActivity.BINDER_FRAG_TAG";

    private BinderFragment binderFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        FragmentManager fm = getFragmentManager();
        //do some initialization stuff...

        Fragment fragment = fm.findFragmentByTag(BINDER_FRAG_TAG);
        if(fragment != null){
            Log.i("asd", "Binder Frag already exists");
            binderFragment = (BinderFragment)fragment;
        }
        else{
            Log.i("asd", "Binder frag is null");
            binderFragment = new BinderFragment();
            fm.beginTransaction().add(binderFragment, BINDER_FRAG_TAG).commit();
        }
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);
    }
}

虽然这是片段:

public class BinderFragment extends Fragment {
    private Application application;

    public BinderFragment(){
        Log.i("asd", "Constructor called for binder fragment");
    }

    @Override
    public void onAttach(Activity activity){
        super.onAttach(activity);
        if(application == null){
            application = activity.getApplication();
            //make connections...
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        setRetainInstance(true);
        super.onCreate(savedInstanceState);
    }

     @Override
     public void onDestroy(){
         Log.i("asd", "binder fragment destroyed");
         super.onDestroy();
         //disconnect services
     }
}

如果我运行应用程序并尝试更改我得到的方向:

<application started>
I/asd﹕ Binder frag is null
I/asd﹕ Constructor called for binder fragment
I/asd﹕ Attaching
<orientation change>
I/asd﹕ Is fragment found? true
I/asd﹕ Binder frag is null
I/asd﹕ Constructor called for binder fragment
I/asd﹕ Attaching
<orientation change>
I/asd﹕ Is fragment found? true
I/asd﹕ Binder frag is null
I/asd﹕ Constructor called for binder fragment
I/asd﹕ Attaching
<application closed>
I/asd﹕ binder fragment destroyed
I/asd﹕ binder fragment destroyed
I/asd﹕ binder fragment destroyed
I/asd﹕ Is fragment found? false

正如您所看到的,Fragment在活动被销毁时存在,并且它被正确保留(因为它们在应用程序关闭时都被销毁),但onCreate找不到它,创建一个新的...任何人都知道为什么我不能通过标签检索片段?

感谢您的帮助^^

1 个答案:

答案 0 :(得分:0)

在这里拍摄,

为什么在Fragment事务提交之后将调用放到supersetContentView。在onCreate方法中,首先调用super然后调用setContentView,然后调用片段事务。

@Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fm = getFragmentManager();
        //do some initialization stuff...

       FragmentManager fragmentManager = getFragmentManager(); //suggestion to use supportFragmentMan
       binderFragment = (BinderFragment) fragmentManager.findFragmentByTag(TAG_SEARCH_ARTIST_FRAGMENT);

       if(binderFragment == null){
            binderFragment = new BinderFragment();
            fragmentManager.beginTransaction().add(binderFragment , BINDER_FRAG_TAG).commit();
       } 
    }

希望这有帮助!