diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java index 0b7446d42..3163b89a7 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentLayout.java @@ -20,25 +20,27 @@ import com.example.android.apis.R; import android.app.Activity; import android.app.Fragment; -import android.app.FragmentTransaction; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.view.View.OnClickListener; -import android.widget.Button; import android.widget.TextView; public class FragmentLayout extends Activity { View mFirstFragmentView; View mSecondFragmentView; + Fragment mFirstFragment; + Fragment mSecondFragment; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_layout); mFirstFragmentView = findViewById(R.id.fragment1); mSecondFragmentView = findViewById(R.id.fragment2); + mFirstFragment = findFragmentById(R.id.fragment1); + mSecondFragment = findFragmentById(R.id.fragment2); } static class FirstFragment extends Fragment { diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java index 568ea93b8..586b4b91b 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentStack.java @@ -30,14 +30,17 @@ import android.widget.Button; import android.widget.TextView; public class FragmentStack extends Activity { - int mStackLevel; - Fragment mLastFragment; + int mStackLevel = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_stack); - addFragmentToStack(); + + // Add initial fragment. + Fragment newFragment = new CountingFragment(mStackLevel); + FragmentTransaction ft = openFragmentTransaction(); + ft.add(newFragment, R.id.simple_fragment).commit(); // Watch for button clicks. Button button = (Button)findViewById(R.id.next); @@ -52,12 +55,9 @@ public class FragmentStack extends Activity { mStackLevel++; Fragment newFragment = new CountingFragment(mStackLevel); FragmentTransaction ft = openFragmentTransaction(); - if (mLastFragment != null) { - ft.remove(mLastFragment); - } - ft.add(newFragment, R.id.simple_fragment); - mLastFragment = newFragment; - ft.addToBackStack(); + ft.replace(newFragment, R.id.simple_fragment); + ft.setTransition(FragmentTransaction.TRANSIT_ACTIVITY_OPEN); + ft.addToBackStack(null); ft.commit(); }