diff --git a/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java b/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java index 95523705a..383242a84 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/FragmentRetainInstance.java @@ -22,7 +22,9 @@ import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; 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.ProgressBar; @@ -34,33 +36,62 @@ import android.widget.ProgressBar; * easier than using the raw Activity.onRetainNonConfiguratinInstance() API. */ public class FragmentRetainInstance extends Activity { - RetainedFragment mRetainedFragment; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); - setContentView(R.layout.fragment_retain_instance); - - // Watch for button clicks. - Button button = (Button)findViewById(R.id.restart); - button.setOnClickListener(new OnClickListener() { - public void onClick(View v) { - mRetainedFragment.restart(); - } - }); - - FragmentManager fm = getFragmentManager(); - // Check to see if we retained the fragment. - mRetainedFragment = (RetainedFragment)fm.findFragmentByTag("retained"); - - // If not retained (or first time running), we need to re-create it. - if (mRetainedFragment == null) { - mRetainedFragment = new RetainedFragment(); - fm.openTransaction().add(mRetainedFragment, "retained").commit(); + // First time init, create the UI. + if (savedInstanceState == null) { + getFragmentManager().openTransaction().add(android.R.id.content, + new UiFragment()).commit(); } } + /** + * This is a fragment showing UI that will be updated from work done + * in the retained fragment. + */ + public static class UiFragment extends Fragment { + RetainedFragment mWorkFragment; + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_retain_instance, container, false); + + // Watch for button clicks. + Button button = (Button)v.findViewById(R.id.restart); + button.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + if (mWorkFragment != null) { + mWorkFragment.restart(); + } + } + }); + + return v; + } + + @Override + public void onActivityCreated(Bundle savedInstanceState) { + super.onActivityCreated(savedInstanceState); + + FragmentManager fm = getFragmentManager(); + + // Check to see if we have retained the worker fragment. + mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work"); + + // If not retained (or first time running), we need to create it. + if (mWorkFragment == null) { + mWorkFragment = new RetainedFragment(); + // Tell it who it is working with. + mWorkFragment.setTargetFragment(this, 0); + fm.openTransaction().add(mWorkFragment, "work").commit(); + } + } + + } + /** * This is the Fragment implementation that will be retained across * activity instances. It represents some ongoing work, here a thread @@ -145,8 +176,8 @@ public class FragmentRetainInstance extends Activity { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - // Retrieve the progress bar from the current activity. - mProgressBar = (ProgressBar)getActivity().findViewById( + // Retrieve the progress bar from the target's view hierarchy. + mProgressBar = (ProgressBar)getTargetFragment().getView().findViewById( R.id.progress_horizontal); // We are ready for our thread to go.