zhihu / RxLifecycle

Bind observables to the lifecycle of Activity or Fragment in a non-invasive way.
516 stars 47 forks source link

fragmentManager.findFragmentByTag sometime cannot find an attaching BindingFragment #8

Open microstudent opened 6 years ago

microstudent commented 6 years ago

when binding on Activity/Fragment onCreate() , Rxlifecycle would add an bindingFragment, look like this:

protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "" + "ActionBarActivity onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jnitest);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment(), "myfragment")
                    .commit();
        }
        Log.d(TAG, "Fragment:"+getSupportFragmentManager().findFragmentById(R.id.container));

findFragmentById will return null. if we bind lifeCycle during onCreate() more than once, which is very common to do such as load a lot of data, it would attach more than one BindingFragment ! and it seems to be wrong.