onAttach有时不会被调用

activity继承自FragmentActivity

Fragment1继承自Fragment

 

在activity中: gettFragmentManager ,并且fragment都是android.app中的

FragmentTransaction fragmentTransaction = 
        getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_layout, new Fragment1(), "frg1");
fragmentTransaction.commit();

 此时fragment中的 onAttach方法是不会调用的,其他方法正常调用

 

换成android.support.v4.app包的getSupportFragmentManager

FragmentTransaction fragmentTransaction = 
       getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.fragment_layout, new Fragment1(), "frg1");
fragmentTransaction.commit();

 

此时 onAttach才会调用 ,其他方法也正常

 

在栈溢出网上也搜到了:http://stackoverflow.com/questions/32077086/android-onattachcontext-not-called-for-api-23

 

 

 

你可能感兴趣的:(onAttach有时不会被调用)