Created
April 18, 2018 13:15
-
-
Save rosuH/6a10b4ef7bfc6aaabbf1823ca76e1e99 to your computer and use it in GitHub Desktop.
A common super class to create activity that hosting a fragment.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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