Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@chrisoverstreet chrisoverstreet commented Apr 2, 2017

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

@chrisoverstreet

This comment has been minimized.

Copy link
Owner Author

@chrisoverstreet chrisoverstreet commented Apr 2, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.