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;
* 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);
+
+ // First time init, create the UI.
+ if (savedInstanceState == null) {
+ getFragmentManager().openTransaction().add(android.R.id.content,
+ new UiFragment()).commit();
+ }
+ }
- // Watch for button clicks.
- Button button = (Button)findViewById(R.id.restart);
- button.setOnClickListener(new OnClickListener() {
- public void onClick(View v) {
- mRetainedFragment.restart();
- }
- });
+ /**
+ * 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;
- FragmentManager fm = getFragmentManager();
-
- // Check to see if we retained the fragment.
- mRetainedFragment = (RetainedFragment)fm.findFragmentByTag("retained");
+ @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();
+ }
+ }
+ });
- // 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();
+ 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();
+ }
+ }
+
}
/**
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.