package com.andrewpham.android.khanacademy_learnanything.controllers; import android.app.ActionBar; import android.graphics.drawable.ColorDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.andrewpham.android.khanacademy_learnanything.R; /** * Created by Andrew on 19/08/2014. */ public abstract class SingleFragmentActivity extends FragmentActivity { private ActionBar mActionBar; protected abstract Fragment createFragment(); protected int getLayoutResId() { return R.layout.activity_fragment; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActionBar = getActionBar(); mActionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.actionbar_background))); mActionBar.setDisplayShowTitleEnabled(false); setContentView(getLayoutResId()); FragmentManager fm = getSupportFragmentManager(); Fragment fragment = fm.findFragmentById(R.id.fragmentContainer); if (fragment == null) { fragment = createFragment(); fm.beginTransaction() .add(R.id.fragmentContainer, fragment) .commit(); } } }