Skip to content

Instantly share code, notes, and snippets.

@rosuH
Created April 18, 2018 13:15
Show Gist options
  • Save rosuH/6a10b4ef7bfc6aaabbf1823ca76e1e99 to your computer and use it in GitHub Desktop.
Save rosuH/6a10b4ef7bfc6aaabbf1823ca76e1e99 to your computer and use it in GitHub Desktop.
A common super class to create activity that hosting a fragment.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragment_container"/>
public abstract class SingleFragmentActivity extends AppCompatActivity {
protected abstract Fragment createFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
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();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment