Modify retain sample to show of retained targets.

Change-Id: I33dd83651006469ef2af7976861f3b832e13bf60
This commit is contained in:
Dianne Hackborn
2010-09-11 20:58:59 -07:00
parent 0e8b60db14
commit 519b6aa41e

View File

@@ -22,7 +22,9 @@ import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentManager; import android.app.FragmentManager;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener; import android.view.View.OnClickListener;
import android.widget.Button; import android.widget.Button;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@@ -34,33 +36,62 @@ import android.widget.ProgressBar;
* easier than using the raw Activity.onRetainNonConfiguratinInstance() API. * easier than using the raw Activity.onRetainNonConfiguratinInstance() API.
*/ */
public class FragmentRetainInstance extends Activity { public class FragmentRetainInstance extends Activity {
RetainedFragment mRetainedFragment;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(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();
}
}
/**
* 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. // Watch for button clicks.
Button button = (Button)findViewById(R.id.restart); Button button = (Button)v.findViewById(R.id.restart);
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
mRetainedFragment.restart(); if (mWorkFragment != null) {
mWorkFragment.restart();
}
} }
}); });
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getFragmentManager(); FragmentManager fm = getFragmentManager();
// Check to see if we retained the fragment. // Check to see if we have retained the worker fragment.
mRetainedFragment = (RetainedFragment)fm.findFragmentByTag("retained"); mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work");
// If not retained (or first time running), we need to re-create it. // If not retained (or first time running), we need to create it.
if (mRetainedFragment == null) { if (mWorkFragment == null) {
mRetainedFragment = new RetainedFragment(); mWorkFragment = new RetainedFragment();
fm.openTransaction().add(mRetainedFragment, "retained").commit(); // 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 * This is the Fragment implementation that will be retained across
* activity instances. It represents some ongoing work, here a thread * activity instances. It represents some ongoing work, here a thread
@@ -145,8 +176,8 @@ public class FragmentRetainInstance extends Activity {
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
// Retrieve the progress bar from the current activity. // Retrieve the progress bar from the target's view hierarchy.
mProgressBar = (ProgressBar)getActivity().findViewById( mProgressBar = (ProgressBar)getTargetFragment().getView().findViewById(
R.id.progress_horizontal); R.id.progress_horizontal);
// We are ready for our thread to go. // We are ready for our thread to go.