Cleanup fragment api demos

* Cleaned up extra whitespace & made vertical whitespace more consistent
* 100col wrap
* Remove unused imports
* Add @Override where necessary

Change-Id: I1ad404ffc4ce7fa9c4b661f7cc1783c86356316b
This commit is contained in:
Andrew Stadler
2010-08-03 17:39:20 -07:00
parent 132ab286a1
commit 45ae9c3def
12 changed files with 89 additions and 75 deletions

View File

@@ -30,6 +30,7 @@ import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
public class FragmentAlertDialog extends Activity { public class FragmentAlertDialog extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -53,21 +54,26 @@ public class FragmentAlertDialog extends Activity {
} }
public static class MyAlertDialogFragment extends DialogFragment { public static class MyAlertDialogFragment extends DialogFragment {
@Override @Override
public Dialog onCreateDialog(Bundle savedInstanceState) { public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity()) return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon) .setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title) .setTitle(R.string.alert_dialog_two_buttons_title)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() { .setPositiveButton(R.string.alert_dialog_ok,
public void onClick(DialogInterface dialog, int whichButton) { new DialogInterface.OnClickListener() {
/* User clicked OK so do some stuff */ public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
} }
}) )
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() { .setNegativeButton(R.string.alert_dialog_cancel,
public void onClick(DialogInterface dialog, int whichButton) { new DialogInterface.OnClickListener() {
/* User clicked Cancel so do some stuff */ public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
}
} }
}) )
.create(); .create();
} }
} }

View File

@@ -34,15 +34,16 @@ import android.widget.TextView;
* Demonstration of animations when changing fragment states. * Demonstration of animations when changing fragment states.
*/ */
public class FragmentAnim extends Activity { public class FragmentAnim extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_anim); setContentView(R.layout.fragment_anim);
addShowHideListener(R.id.frag1hide, findFragmentById(R.id.fragment1)); addShowHideListener(R.id.frag1hide, findFragmentById(R.id.fragment1));
addShowHideListener(R.id.frag2hide, findFragmentById(R.id.fragment2)); addShowHideListener(R.id.frag2hide, findFragmentById(R.id.fragment2));
} }
void addShowHideListener(int buttonId, final Fragment fragment) { void addShowHideListener(int buttonId, final Fragment fragment) {
final Button button = (Button)findViewById(buttonId); final Button button = (Button)findViewById(buttonId);
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
@@ -62,16 +63,17 @@ public class FragmentAnim extends Activity {
} }
}); });
} }
public static class FirstFragment extends Fragment { public static class FirstFragment extends Fragment {
TextView mTextView; TextView mTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.labeled_text_edit, container, false); View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findViewById(R.id.msg); View tv = v.findViewById(R.id.msg);
((TextView)tv).setText("The fragment saves and restores this text."); ((TextView)tv).setText("The fragment saves and restores this text.");
// Retrieve the text editor, and restore the last saved state if needed. // Retrieve the text editor, and restore the last saved state if needed.
mTextView = (TextView)v.findViewById(R.id.saved); mTextView = (TextView)v.findViewById(R.id.saved);
if (savedInstanceState != null) { if (savedInstanceState != null) {
@@ -83,19 +85,21 @@ public class FragmentAnim extends Activity {
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
// Remember the current text, to restore if we later restart. // Remember the current text, to restore if we later restart.
outState.putCharSequence("text", mTextView.getText()); outState.putCharSequence("text", mTextView.getText());
} }
} }
public static class SecondFragment extends Fragment { public static class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.labeled_text_edit, container, false); View v = inflater.inflate(R.layout.labeled_text_edit, container, false);
View tv = v.findViewById(R.id.msg); View tv = v.findViewById(R.id.msg);
((TextView)tv).setText("The TextView saves and restores this text."); ((TextView)tv).setText("The TextView saves and restores this text.");
// Retrieve the text editor and tell it to save and restore its state. // Retrieve the text editor and tell it to save and restore its state.
// Note that you will often set this in the layout XML, but since // Note that you will often set this in the layout XML, but since
// we are sharing our layout with the other fragment we will customize // we are sharing our layout with the other fragment we will customize

View File

@@ -34,16 +34,18 @@ import android.view.ContextMenu.ContextMenuInfo;
* Demonstration of displaying a context menu from a fragment. * Demonstration of displaying a context menu from a fragment.
*/ */
public class FragmentContextMenu extends Activity { public class FragmentContextMenu extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content. // Create the list fragment and add it as our sole content.
ContextMenuFragment content = new ContextMenuFragment(); ContextMenuFragment content = new ContextMenuFragment();
openFragmentTransaction().add(android.R.id.content, content).commit(); openFragmentTransaction().add(android.R.id.content, content).commit();
} }
public static class ContextMenuFragment extends Fragment { public static class ContextMenuFragment extends Fragment {
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
@@ -58,7 +60,7 @@ public class FragmentContextMenu extends Activity {
menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A"); menu.add(Menu.NONE, R.id.a_item, Menu.NONE, "Menu A");
menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B"); menu.add(Menu.NONE, R.id.b_item, Menu.NONE, "Menu B");
} }
@Override @Override
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) { switch (item.getItemId()) {

View File

@@ -132,6 +132,7 @@ public class FragmentDialog extends Activity {
outState.putInt("num", mNum); outState.putInt("num", mNum);
} }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_dialog, container, false); View v = inflater.inflate(R.layout.fragment_dialog, container, false);

View File

@@ -21,18 +21,14 @@ import com.example.android.apis.Shakespeare;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Intent; import android.content.Intent;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView; import android.widget.ListView;
import android.widget.ScrollView; import android.widget.ScrollView;
import android.widget.TextView; import android.widget.TextView;
@@ -43,6 +39,7 @@ import android.widget.TextView;
* landscape. * landscape.
*/ */
public class FragmentLayout extends Activity { public class FragmentLayout extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -50,16 +47,17 @@ public class FragmentLayout extends Activity {
// This layout varies depending on the screen size. // This layout varies depending on the screen size.
setContentView(R.layout.fragment_layout); setContentView(R.layout.fragment_layout);
} }
/** /**
* This is a secondary activity, to show what the user has selected * This is a secondary activity, to show what the user has selected
* when the screen is not large enough to show it all in one activity. * when the screen is not large enough to show it all in one activity.
*/ */
public static class DialogActivity extends Activity { public static class DialogActivity extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
if (getResources().getConfiguration().orientation if (getResources().getConfiguration().orientation
== Configuration.ORIENTATION_LANDSCAPE) { == Configuration.ORIENTATION_LANDSCAPE) {
// If the screen is now in landscape mode, we can show the // If the screen is now in landscape mode, we can show the
@@ -67,15 +65,16 @@ public class FragmentLayout extends Activity {
finish(); finish();
return; return;
} }
DialogFragment dialog = new DialogFragment(); DialogFragment dialog = new DialogFragment();
openFragmentTransaction().add(android.R.id.content, dialog).commit(); openFragmentTransaction().add(android.R.id.content, dialog).commit();
dialog.setText(getIntent().getIntExtra("text", -1)); dialog.setText(getIntent().getIntExtra("text", -1));
} }
} }
public static class TitlesFragment extends Fragment public static class TitlesFragment extends Fragment
implements AdapterView.OnItemClickListener { implements AdapterView.OnItemClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
ListView list = new ListView(getActivity()); ListView list = new ListView(getActivity());
@@ -99,18 +98,18 @@ public class FragmentLayout extends Activity {
} }
} }
} }
public static class DialogFragment extends Fragment { public static class DialogFragment extends Fragment {
int mDisplayedText = -1; int mDisplayedText = -1;
TextView mText; TextView mText;
public void setText(int id) { public void setText(int id) {
mDisplayedText = id; mDisplayedText = id;
if (mText != null && id >= 0) { if (mText != null && id >= 0) {
mText.setText(Shakespeare.DIALOGUE[id]); mText.setText(Shakespeare.DIALOGUE[id]);
} }
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -125,6 +124,7 @@ public class FragmentLayout extends Activity {
outState.putInt("text", mDisplayedText); outState.putInt("text", mDisplayedText);
} }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
ScrollView scroller = new ScrollView(getActivity()); ScrollView scroller = new ScrollView(getActivity());

View File

@@ -31,25 +31,27 @@ import android.widget.ListView;
* from a canned array. * from a canned array.
*/ */
public class FragmentListArray extends Activity { public class FragmentListArray extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
// Create the list fragment and add it as our sole content. // Create the list fragment and add it as our sole content.
if (findFragmentById(android.R.id.content) == null) { if (findFragmentById(android.R.id.content) == null) {
ArrayListFragment list = new ArrayListFragment(); ArrayListFragment list = new ArrayListFragment();
openFragmentTransaction().add(android.R.id.content, list).commit(); openFragmentTransaction().add(android.R.id.content, list).commit();
} }
} }
public static class ArrayListFragment extends ListFragment { public static class ArrayListFragment extends ListFragment {
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(), setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, Shakespeare.TITLES)); android.R.layout.simple_list_item_1, Shakespeare.TITLES));
} }
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id); Log.i("FragmentList", "Item clicked: " + id);

View File

@@ -23,7 +23,6 @@ import android.content.CursorLoader;
import android.content.Loader; import android.content.Loader;
import android.database.Cursor; import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.provider.Contacts.Phones;
import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Contacts;
import android.util.Log; import android.util.Log;
import android.view.View; import android.view.View;
@@ -36,6 +35,7 @@ import android.widget.SimpleCursorAdapter;
* an empty view and loading progress. * an empty view and loading progress.
*/ */
public class FragmentListCursorLoader extends Activity { public class FragmentListCursorLoader extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -46,7 +46,7 @@ public class FragmentListCursorLoader extends Activity {
openFragmentTransaction().add(android.R.id.content, list).commit(); openFragmentTransaction().add(android.R.id.content, list).commit();
} }
} }
public static class CursorLoaderListFragment extends ListFragment public static class CursorLoaderListFragment extends ListFragment
implements LoaderManager.LoaderCallbacks<Cursor> { implements LoaderManager.LoaderCallbacks<Cursor> {
@Override @Override
@@ -61,12 +61,12 @@ public class FragmentListCursorLoader extends Activity {
// or start a new one. // or start a new one.
getLoaderManager().initLoader(0, null, this); getLoaderManager().initLoader(0, null, this);
} }
@Override @Override
public void onListItemClick(ListView l, View v, int position, long id) { public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentComplexList", "Item clicked: " + id); Log.i("FragmentComplexList", "Item clicked: " + id);
} }
static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] { static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
Contacts._ID, Contacts._ID,
Contacts.DISPLAY_NAME, Contacts.DISPLAY_NAME,
@@ -75,7 +75,7 @@ public class FragmentListCursorLoader extends Activity {
Contacts.PHOTO_ID, Contacts.PHOTO_ID,
Contacts.LOOKUP_KEY, Contacts.LOOKUP_KEY,
}; };
@Override @Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) { public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND (" String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("

View File

@@ -6,15 +6,11 @@ import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuInflater; import android.view.MenuInflater;
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.CheckBox; import android.widget.CheckBox;
import android.widget.TextView;
/** /**
* Demonstrates how fragments can participate in the options menu. * Demonstrates how fragments can participate in the options menu.
@@ -24,14 +20,14 @@ public class FragmentMenu extends Activity {
Fragment mFragment2; Fragment mFragment2;
CheckBox mCheckBox1; CheckBox mCheckBox1;
CheckBox mCheckBox2; CheckBox mCheckBox2;
// Update fragment visibility when check boxes are changed. // Update fragment visibility when check boxes are changed.
final OnClickListener mClickListener = new OnClickListener() { final OnClickListener mClickListener = new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
updateFragmentVisibility(); updateFragmentVisibility();
} }
}; };
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -60,7 +56,7 @@ public class FragmentMenu extends Activity {
// Make sure fragments start out with correct visibility. // Make sure fragments start out with correct visibility.
updateFragmentVisibility(); updateFragmentVisibility();
} }
@Override @Override
protected void onRestoreInstanceState(Bundle savedInstanceState) { protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState); super.onRestoreInstanceState(savedInstanceState);
@@ -77,13 +73,14 @@ public class FragmentMenu extends Activity {
else ft.hide(mFragment2); else ft.hide(mFragment2);
ft.commit(); ft.commit();
} }
/** /**
* A fragment that displays a menu. This fragment happens to not * A fragment that displays a menu. This fragment happens to not
* have a UI (it does not implement onCreateView), but it could also * have a UI (it does not implement onCreateView), but it could also
* have one if it wanted. * have one if it wanted.
*/ */
public static class MenuFragment extends Fragment { public static class MenuFragment extends Fragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -101,6 +98,7 @@ public class FragmentMenu extends Activity {
* Second fragment with a menu. * Second fragment with a menu.
*/ */
public static class Menu2Fragment extends Fragment { public static class Menu2Fragment extends Fragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);

View File

@@ -1,7 +1,6 @@
package com.example.android.apis.app; package com.example.android.apis.app;
import com.example.android.apis.R; import com.example.android.apis.R;
import com.example.android.apis.app.FragmentStack.CountingFragment;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
@@ -11,13 +10,14 @@ import android.os.Bundle;
import android.text.Editable; import android.text.Editable;
import android.view.LayoutInflater; 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.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.TextView; import android.widget.TextView;
public class FragmentReceiveResult extends Activity { public class FragmentReceiveResult extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -35,13 +35,13 @@ public class FragmentReceiveResult extends Activity {
ft.add(R.id.simple_fragment, newFragment).commit(); ft.add(R.id.simple_fragment, newFragment).commit();
} }
} }
public static class ReceiveResultFragment extends Fragment { public static class ReceiveResultFragment extends Fragment {
// Definition of the one requestCode we use for receiving resuls. // Definition of the one requestCode we use for receiving resuls.
static final private int GET_CODE = 0; static final private int GET_CODE = 0;
private TextView mResults; private TextView mResults;
private OnClickListener mGetListener = new OnClickListener() { private OnClickListener mGetListener = new OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
// Start the activity whose result we want to retrieve. The // Start the activity whose result we want to retrieve. The
@@ -61,6 +61,7 @@ public class FragmentReceiveResult extends Activity {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
} }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.receive_result, container, false); View v = inflater.inflate(R.layout.receive_result, container, false);
@@ -77,7 +78,7 @@ public class FragmentReceiveResult extends Activity {
return v; return v;
} }
/** /**
* This method is called when the sending activity has finished, with the * This method is called when the sending activity has finished, with the
* result it supplied. * result it supplied.
@@ -112,6 +113,5 @@ public class FragmentReceiveResult extends Activity {
text.append("\n"); text.append("\n");
} }
} }
} }
} }

View File

@@ -20,15 +20,11 @@ import com.example.android.apis.R;
import android.app.Activity; import android.app.Activity;
import android.app.Fragment; import android.app.Fragment;
import android.app.FragmentTransaction;
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;
import android.widget.TextView;
/** /**
* This example shows how you can use a Fragment to easily propagate state * This example shows how you can use a Fragment to easily propagate state
@@ -38,12 +34,12 @@ import android.widget.TextView;
*/ */
public class FragmentRetainInstance extends Activity { public class FragmentRetainInstance extends Activity {
RetainedFragment mRetainedFragment; 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); setContentView(R.layout.fragment_retain_instance);
// Watch for button clicks. // Watch for button clicks.
Button button = (Button)findViewById(R.id.restart); Button button = (Button)findViewById(R.id.restart);
button.setOnClickListener(new OnClickListener() { button.setOnClickListener(new OnClickListener() {
@@ -51,17 +47,17 @@ public class FragmentRetainInstance extends Activity {
mRetainedFragment.restart(); mRetainedFragment.restart();
} }
}); });
// Check to see if we retained the fragment. // Check to see if we retained the fragment.
mRetainedFragment = (RetainedFragment)findFragmentByTag("retained"); mRetainedFragment = (RetainedFragment)findFragmentByTag("retained");
// If not retained (or first time running), we need to re-create it. // If not retained (or first time running), we need to re-create it.
if (mRetainedFragment == null) { if (mRetainedFragment == null) {
mRetainedFragment = new RetainedFragment(); mRetainedFragment = new RetainedFragment();
openFragmentTransaction().add(mRetainedFragment, "retained").commit(); openFragmentTransaction().add(mRetainedFragment, "retained").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
@@ -72,7 +68,7 @@ public class FragmentRetainInstance extends Activity {
int mPosition; int mPosition;
boolean mReady = false; boolean mReady = false;
boolean mQuiting = false; boolean mQuiting = false;
/** /**
* This is the thread that will do our work. It sits in a loop running * This is the thread that will do our work. It sits in a loop running
* the progress up until it has reached the top, then stops and waits. * the progress up until it has reached the top, then stops and waits.
@@ -82,10 +78,10 @@ public class FragmentRetainInstance extends Activity {
public void run() { public void run() {
// We'll figure the real value out later. // We'll figure the real value out later.
int max = 10000; int max = 10000;
// This thread runs almost forever. // This thread runs almost forever.
while (true) { while (true) {
// Update our shared state with the UI. // Update our shared state with the UI.
synchronized (this) { synchronized (this) {
// Our thread is stopped if the UI is not ready // Our thread is stopped if the UI is not ready
@@ -99,7 +95,7 @@ public class FragmentRetainInstance extends Activity {
} catch (InterruptedException e) { } catch (InterruptedException e) {
} }
} }
// Now update the progress. Note it is important that // Now update the progress. Note it is important that
// we touch the progress bar with the lock held, so it // we touch the progress bar with the lock held, so it
// doesn't disappear on us. // doesn't disappear on us.
@@ -107,7 +103,7 @@ public class FragmentRetainInstance extends Activity {
max = mProgressBar.getMax(); max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition); mProgressBar.setProgress(mPosition);
} }
// Normally we would be doing some work, but put a kludge // Normally we would be doing some work, but put a kludge
// here to pretend like we are. // here to pretend like we are.
synchronized (this) { synchronized (this) {
@@ -119,7 +115,7 @@ public class FragmentRetainInstance extends Activity {
} }
} }
}; };
/** /**
* Fragment initialization. We way we want to be retained and * Fragment initialization. We way we want to be retained and
* start our thread. * start our thread.
@@ -190,7 +186,7 @@ public class FragmentRetainInstance extends Activity {
super.onDetach(); super.onDetach();
} }
/** /**
* API for our UI to restart the progress thread. * API for our UI to restart the progress thread.
*/ */

View File

@@ -31,7 +31,7 @@ import android.widget.TextView;
public class FragmentStack extends Activity { public class FragmentStack extends Activity {
int mStackLevel = 1; int mStackLevel = 1;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -54,7 +54,7 @@ public class FragmentStack extends Activity {
mStackLevel = savedInstanceState.getInt("level"); mStackLevel = savedInstanceState.getInt("level");
} }
} }
@Override @Override
public void onSaveInstanceState(Bundle outState) { public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
@@ -70,18 +70,18 @@ public class FragmentStack extends Activity {
ft.addToBackStack(null); ft.addToBackStack(null);
ft.commit(); ft.commit();
} }
public static class CountingFragment extends Fragment { public static class CountingFragment extends Fragment {
int mNum; int mNum;
public CountingFragment() { public CountingFragment() {
mNum = -1; mNum = -1;
} }
public CountingFragment(int num) { public CountingFragment(int num) {
mNum = num; mNum = num;
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -96,6 +96,7 @@ public class FragmentStack extends Activity {
outState.putInt("num", mNum); outState.putInt("num", mNum);
} }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.hello_world, container, false); View v = inflater.inflate(R.layout.hello_world, container, false);

View File

@@ -33,6 +33,7 @@ import android.widget.ListView;
*/ */
//BEGIN_INCLUDE(activity) //BEGIN_INCLUDE(activity)
public class FragmentPreferences extends Activity { public class FragmentPreferences extends Activity {
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -47,6 +48,7 @@ public class FragmentPreferences extends Activity {
* pick one, the corresponding preferences fragment is created and shown. * pick one, the corresponding preferences fragment is created and shown.
*/ */
public static class CategoriesFragment extends ListFragment { public static class CategoriesFragment extends ListFragment {
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
@@ -72,6 +74,7 @@ public class FragmentPreferences extends Activity {
//BEGIN_INCLUDE(fragment) //BEGIN_INCLUDE(fragment)
public static class Prefs1Fragment extends PreferenceFragment { public static class Prefs1Fragment extends PreferenceFragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -83,6 +86,7 @@ public class FragmentPreferences extends Activity {
//END_INCLUDE(fragment) //END_INCLUDE(fragment)
public static class Prefs2Fragment extends PreferenceFragment { public static class Prefs2Fragment extends PreferenceFragment {
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);