Update to follow fragment API changes.

Also make use of fragment arguments and targets to simplify code.

Change-Id: I79884854f4c7ff4fdc35c68087b5b2235a75c79f
This commit is contained in:
Dianne Hackborn
2010-08-15 12:49:50 -07:00
parent b880eb4260
commit 9e6f12a598
15 changed files with 72 additions and 30 deletions

View File

@@ -24,6 +24,7 @@ import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
@@ -49,28 +50,49 @@ public class FragmentAlertDialog extends Activity {
}
void showDialog() {
DialogFragment newFragment = new MyAlertDialogFragment();
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
R.string.alert_dialog_two_buttons_title);
newFragment.show(this, "dialog");
}
public void doPositiveClick() {
// Do stuff here.
Log.i("FragmentAlertDialog", "Positive click!");
}
public void doNegativeClick() {
// Do stuff here.
Log.i("FragmentAlertDialog", "Negative click!");
}
public static class MyAlertDialogFragment extends DialogFragment {
public static MyAlertDialogFragment newInstance(int title) {
MyAlertDialogFragment frag = new MyAlertDialogFragment();
Bundle args = new Bundle();
args.putInt("title", title);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int title = getArguments().getInt("title");
return new AlertDialog.Builder(getActivity())
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_two_buttons_title)
.setTitle(title)
.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
((FragmentAlertDialog)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Cancel so do some stuff */
((FragmentAlertDialog)getActivity()).doNegativeClick();
}
}
)

View File

@@ -41,7 +41,7 @@ public class FragmentContextMenu extends Activity {
// Create the list fragment and add it as our sole content.
ContextMenuFragment content = new ContextMenuFragment();
openFragmentTransaction().add(android.R.id.content, content).commit();
getFragmentManager().openTransaction().add(android.R.id.content, content).commit();
}
public static class ContextMenuFragment extends Fragment {

View File

@@ -69,8 +69,8 @@ public class FragmentDialog extends Activity {
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
FragmentTransaction ft = openFragmentTransaction();
Fragment prev = findFragmentByTag("dialog");
FragmentTransaction ft = getFragmentManager().openTransaction();
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
if (prev != null) {
ft.remove(prev);
}

View File

@@ -4,6 +4,7 @@ import com.example.android.apis.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.Menu;
@@ -35,13 +36,14 @@ public class FragmentMenu extends Activity {
setContentView(R.layout.fragment_menu);
// Make sure the two menu fragments are created.
FragmentTransaction ft = openFragmentTransaction();
mFragment1 = findFragmentByTag("f1");
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.openTransaction();
mFragment1 = fm.findFragmentByTag("f1");
if (mFragment1 == null) {
mFragment1 = new MenuFragment();
ft.add(mFragment1, "f1");
}
mFragment2 = findFragmentByTag("f2");
mFragment2 = fm.findFragmentByTag("f2");
if (mFragment2 == null) {
mFragment2 = new Menu2Fragment();
ft.add(mFragment2, "f2");
@@ -67,7 +69,7 @@ public class FragmentMenu extends Activity {
// Update fragment visibility based on current check box state.
void updateFragmentVisibility() {
FragmentTransaction ft = openFragmentTransaction();
FragmentTransaction ft = getFragmentManager().openTransaction();
if (mCheckBox1.isChecked()) ft.show(mFragment1);
else ft.hide(mFragment1);
if (mCheckBox2.isChecked()) ft.show(mFragment2);
@@ -90,8 +92,8 @@ public class FragmentMenu extends Activity {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Menu 1a").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
@@ -108,7 +110,7 @@ public class FragmentMenu extends Activity {
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.add("Menu 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
menu.add("Menu 2").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
}
}
}

View File

@@ -31,7 +31,7 @@ public class FragmentReceiveResult extends Activity {
if (savedInstanceState == null) {
// Do first time initialization -- add fragment.
Fragment newFragment = new ReceiveResultFragment();
FragmentTransaction ft = openFragmentTransaction();
FragmentTransaction ft = getFragmentManager().openTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
}
}

View File

@@ -20,6 +20,7 @@ import com.example.android.apis.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
@@ -48,13 +49,15 @@ public class FragmentRetainInstance extends Activity {
}
});
FragmentManager fm = getFragmentManager();
// Check to see if we retained the fragment.
mRetainedFragment = (RetainedFragment)findFragmentByTag("retained");
mRetainedFragment = (RetainedFragment)fm.findFragmentByTag("retained");
// If not retained (or first time running), we need to re-create it.
if (mRetainedFragment == null) {
mRetainedFragment = new RetainedFragment();
openFragmentTransaction().add(mRetainedFragment, "retained").commit();
fm.openTransaction().add(mRetainedFragment, "retained").commit();
}
}

View File

@@ -48,7 +48,7 @@ public class FragmentStack extends Activity {
if (savedInstanceState == null) {
// Do first time initialization -- add initial fragment.
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
FragmentTransaction ft = openFragmentTransaction();
FragmentTransaction ft = getFragmentManager().openTransaction();
ft.add(R.id.simple_fragment, newFragment).commit();
} else {
mStackLevel = savedInstanceState.getInt("level");
@@ -69,7 +69,7 @@ public class FragmentStack extends Activity {
// Add the fragment to the activity, pushing this transaction
// on to the back stack.
FragmentTransaction ft = openFragmentTransaction();
FragmentTransaction ft = getFragmentManager().openTransaction();
ft.replace(R.id.simple_fragment, newFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);