getActivity()方法可以在fragment中獲取activity的句柄進(jìn)行下一步操作,然而getActivity取出的值有時會為空冀泻,這里面會產(chǎn)生一個時序問題住涉,也就是activity還沒有創(chuàng)建完战惊,就在fragment中使用
public class TestFragment extends Fragment {
private static final String TAG = "Fragment";
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View view = inflater.inflate(R.layout.fragment_test, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d(TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.d(TAG, "onDetach");
}
}
- onAttach方法:Fragment和Activity建立關(guān)聯(lián)的時候調(diào)用缩挑。
- onCreateView方法:為Fragment加載布局時調(diào)用但两。
- onActivityCreated方法:當(dāng)Activity中的onCreate方法執(zhí)行完后調(diào)用。
- onDestroyView方法:Fragment中的布局被移除時調(diào)用调煎。
- onDetach方法:Fragment和Activity解除關(guān)聯(lián)的時候調(diào)用镜遣。
打印一下生命周期函數(shù)
從上圖執(zhí)行順序看來己肮,在fragment中的onCreateView 中執(zhí)行后續(xù)操作是不保險的士袄,因為activity的onCreate方法沒有執(zhí)行完悲关,所有g(shù)etActivity方法就有可能為null,
小小結(jié):在fragment 的onCreateView中不做太多的操作,具體后續(xù)操作(initView娄柳, initData等均放到onActivityCreated中)
fragment和activity之間通信
-
Activity 傳遞參數(shù)到fragment中
//activity 中設(shè)置參數(shù) TestFragment newFragment = new TestFragment (); Bundle args = new Bundle(); args.putInt(TestFragment .ARG_POSITION, position); newFragment.setArguments(args); //fragment 中獲取參數(shù) Bundle bundle = getArguments();
-
Fragment 傳遞參數(shù)到Activity中
// fragment 中定義一個接口 public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; public interface OnHeadlineSelectedListener { public void onArticleSelected(int position); } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } } //觸發(fā)事件是調(diào)用 if(mCallback != null) { mCallback.onArticleSelected(1); } //activity 中實現(xiàn)OnHeadlineSelectedListener 接口寓辱,就可以獲得fragment傳遞過來的參數(shù)
backstack 問題
fragment add方發(fā)后沒有調(diào)用addToBackStack, 和調(diào)用addToBackStack的數(shù)量問題
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
BKPaymentFragment paymentFragment = new BKPaymentFragment();
ft.add(getViewId("payment_content"), paymentFragment).commit();
//ft.add(getViewId("payment_content"), paymentFragment).addToBackStack(null).commit();
int num = getSupportFragmentManager().getBackStackEntryCount();
此時獲得的num 數(shù)量均為0
@Override
public void onBackPressed() {
int num = getSupportFragmentManager().getBackStackEntryCount();
LogUtils.d("back fragment 數(shù)量" + num);
super.onBackPressed();
}
此時獲得的num 數(shù)量 沒有調(diào)用addToBackStack的為0赤拒, 調(diào)用過的為1