Modify retain sample to show of retained targets.
Change-Id: I33dd83651006469ef2af7976861f3b832e13bf60
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user