activityからfragmentを起動する

2508 ワード

protected void onCreate(Bundle savedInstanceState) {
   Log.i(TAG,this.toString());
    super.onCreate(savedInstanceState);
    setContentView(getLayoutResId());
    FragmentManager fm=getSupportFragmentManager();
    Log.i(TAG,"fm   :"+fm);
    Fragment fragment=fm.findFragmentById(R.id.fragmentContainer);
    if (fragment==null){
        fragment=createFragment();
        Log.i(TAG,"new frament before commit:"+fragment);
        fm.beginTransaction().add(R.id.fragmentContainer,fragment).commit();
        Log.i(TAG,"new frament after commit:"+fragment);
    }else {Log.i(TAG,"existed frament:"+fragment);}
}