Skip to content

Instantly share code, notes, and snippets.

@ImDevinC
Created May 1, 2014 20:06
Show Gist options
  • Save ImDevinC/54bb7f750642a7804cc2 to your computer and use it in GitHub Desktop.
Save ImDevinC/54bb7f750642a7804cc2 to your computer and use it in GitHub Desktop.
Fragment replace issue
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (savedInstanceState == null) {
ssf = new StartStopFragment();
tkf = new TimeKeeperFragment();
rf = new ResetFragment();
lcf = new LapCounterFragment();
} else {
ssf = (StartStopFragment) getFragmentManager().findFragmentByTag(StartStopFragment.class.getName());
tkf = (TimeKeeperFragment) getFragmentManager().findFragmentByTag(TimeKeeperFragment.class.getName());
rf = (ResetFragment) getFragmentManager().findFragmentByTag(ResetFragment.class.getName());
lcf = (LapCounterFragment) getFragmentManager().findFragmentByTag(LapCounterFragment.class.getName());
}
getFragmentManager().beginTransaction()
.replace(R.id.flStartFrame, ssf, StartStopFragment.class.getName())
.replace(R.id.flTimeKeeperFrame, tkf, TimeKeeperFragment.class.getName())
.replace(R.id.flResetFrame, rf, ResetFragment.class.getName())
.replace(R.id.flLapCounterFrame, lcf, LapCounterFragment.class.getName())
.commit();
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<LinearLayout
android:id="@+id/llTopContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".4"
android:orientation="horizontal"
android:baselineAligned="false">
<FrameLayout
android:id="@+id/flResetFrame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
tools:layout="@layout/fragment_reset"/>
<FrameLayout
android:id="@+id/flLapCounterFrame"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5"
tools:layout="@layout/fragment_lapcount"/>
</LinearLayout>
<FrameLayout
android:id="@+id/flTimeKeeperFrame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".2"
tools:layout="@layout/fragment_time"/>
<FrameLayout
android:id="@+id/flStartFrame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".4"
tools:layout="@layout/fragment_startstop"/>
</LinearLayout>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment