Skip to content

Instantly share code, notes, and snippets.

@chrisoverstreet
Created April 2, 2017 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisoverstreet/21f2b5ef123baa31a2cc3b02648f537d to your computer and use it in GitHub Desktop.
Save chrisoverstreet/21f2b5ef123baa31a2cc3b02648f537d to your computer and use it in GitHub Desktop.
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@LayoutRes
protected int getLayoutResId() {
return R.layout.activity_fragment;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResId());
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
}
@chrisoverstreet
Copy link
Author

from Android Programming: The Big Nerd Ranch Guide (2nd Edition)

@chrisoverstreet
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment