Update API demos to use new Fragment features.
In particular, use the new argument Bundle for passing runtime data to fragments. Simplifies the code some. Change-Id: I248bfa0bf94b1599926038174baa4c7387ed8ba1
This commit is contained in:
@@ -65,35 +65,22 @@ public class FragmentDialog extends Activity {
|
|||||||
|
|
||||||
void showDialog() {
|
void showDialog() {
|
||||||
mStackLevel++;
|
mStackLevel++;
|
||||||
DialogFragment newFragment = new MyDialogFragment(mStackLevel);
|
|
||||||
|
// 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();
|
FragmentTransaction ft = openFragmentTransaction();
|
||||||
Fragment prev = findFragmentByTag("dialog");
|
Fragment prev = findFragmentByTag("dialog");
|
||||||
if (prev != null) {
|
if (prev != null) {
|
||||||
ft.remove(prev);
|
ft.remove(prev);
|
||||||
}
|
}
|
||||||
ft.addToBackStack(null);
|
ft.addToBackStack(null);
|
||||||
|
|
||||||
|
// Create and show the dialog.
|
||||||
|
DialogFragment newFragment = MyDialogFragment.newInstance(mStackLevel);
|
||||||
newFragment.show(this, ft, "dialog");
|
newFragment.show(this, ft, "dialog");
|
||||||
}
|
}
|
||||||
|
|
||||||
static int getStyleForNum(int num) {
|
|
||||||
switch ((num-1)%6) {
|
|
||||||
case 1: return DialogFragment.STYLE_NO_TITLE;
|
|
||||||
case 2: return DialogFragment.STYLE_NO_FRAME;
|
|
||||||
case 3: return DialogFragment.STYLE_NO_INPUT;
|
|
||||||
case 4: return DialogFragment.STYLE_NORMAL;
|
|
||||||
case 5: return DialogFragment.STYLE_NORMAL;
|
|
||||||
}
|
|
||||||
return DialogFragment.STYLE_NORMAL;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int getThemeForNum(int num) {
|
|
||||||
switch ((num-1)%6) {
|
|
||||||
case 4: return android.R.style.Theme_Light;
|
|
||||||
case 5: return android.R.style.Theme;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static String getNameForNum(int num) {
|
static String getNameForNum(int num) {
|
||||||
switch ((num-1)%6) {
|
switch ((num-1)%6) {
|
||||||
case 1: return "STYLE_NO_TITLE";
|
case 1: return "STYLE_NO_TITLE";
|
||||||
@@ -109,27 +96,40 @@ public class FragmentDialog extends Activity {
|
|||||||
public static class MyDialogFragment extends DialogFragment {
|
public static class MyDialogFragment extends DialogFragment {
|
||||||
int mNum;
|
int mNum;
|
||||||
|
|
||||||
public MyDialogFragment() {
|
/**
|
||||||
mNum = -1;
|
* Create a new instance of MyDialogFragment, providing "num"
|
||||||
}
|
* as an argument.
|
||||||
|
*/
|
||||||
|
static MyDialogFragment newInstance(int num) {
|
||||||
|
MyDialogFragment f = new MyDialogFragment();
|
||||||
|
|
||||||
public MyDialogFragment(int num) {
|
// Supply num input as an argument.
|
||||||
super(getStyleForNum(num), getThemeForNum(num));
|
Bundle args = new Bundle();
|
||||||
mNum = num;
|
args.putInt("num", num);
|
||||||
|
f.setArguments(args);
|
||||||
|
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
if (savedInstanceState != null) {
|
mNum = getArguments().getInt("num");
|
||||||
mNum = savedInstanceState.getInt("num");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
// Pick a style based on the num.
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
int style = DialogFragment.STYLE_NORMAL, theme = 0;
|
||||||
super.onSaveInstanceState(outState);
|
switch ((mNum-1)%6) {
|
||||||
outState.putInt("num", mNum);
|
case 1: style = DialogFragment.STYLE_NO_TITLE; break;
|
||||||
|
case 2: style = DialogFragment.STYLE_NO_FRAME; break;
|
||||||
|
case 3: style = DialogFragment.STYLE_NO_INPUT; break;
|
||||||
|
case 4: style = DialogFragment.STYLE_NORMAL; break;
|
||||||
|
case 5: style = DialogFragment.STYLE_NORMAL; break;
|
||||||
|
}
|
||||||
|
switch ((mNum-1)%6) {
|
||||||
|
case 4: theme = android.R.style.Theme_Light; break;
|
||||||
|
case 5: theme = android.R.style.Theme; break;
|
||||||
|
}
|
||||||
|
setStyle(style, theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ public class FragmentStack extends Activity {
|
|||||||
|
|
||||||
if (savedInstanceState == null) {
|
if (savedInstanceState == null) {
|
||||||
// Do first time initialization -- add initial fragment.
|
// Do first time initialization -- add initial fragment.
|
||||||
Fragment newFragment = new CountingFragment(mStackLevel);
|
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = openFragmentTransaction();
|
||||||
ft.add(R.id.simple_fragment, newFragment).commit();
|
ft.add(R.id.simple_fragment, newFragment).commit();
|
||||||
} else {
|
} else {
|
||||||
@@ -63,7 +63,12 @@ public class FragmentStack extends Activity {
|
|||||||
|
|
||||||
void addFragmentToStack() {
|
void addFragmentToStack() {
|
||||||
mStackLevel++;
|
mStackLevel++;
|
||||||
Fragment newFragment = new CountingFragment(mStackLevel);
|
|
||||||
|
// Instantiate a new fragment.
|
||||||
|
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
|
||||||
|
|
||||||
|
// Add the fragment to the activity, pushing this transaction
|
||||||
|
// on to the back stack.
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = openFragmentTransaction();
|
||||||
ft.replace(R.id.simple_fragment, newFragment);
|
ft.replace(R.id.simple_fragment, newFragment);
|
||||||
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
|
||||||
@@ -74,26 +79,25 @@ public class FragmentStack extends Activity {
|
|||||||
public static class CountingFragment extends Fragment {
|
public static class CountingFragment extends Fragment {
|
||||||
int mNum;
|
int mNum;
|
||||||
|
|
||||||
public CountingFragment() {
|
/**
|
||||||
mNum = -1;
|
* Create a new instance of CountingFragment, providing "num"
|
||||||
}
|
* as an argument.
|
||||||
|
*/
|
||||||
|
static CountingFragment newInstance(int num) {
|
||||||
|
CountingFragment f = new CountingFragment();
|
||||||
|
|
||||||
public CountingFragment(int num) {
|
// Supply num input as an argument.
|
||||||
mNum = num;
|
Bundle args = new Bundle();
|
||||||
|
args.putInt("num", num);
|
||||||
|
f.setArguments(args);
|
||||||
|
|
||||||
|
return f;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
if (savedInstanceState != null) {
|
mNum = getArguments().getInt("num");
|
||||||
mNum = savedInstanceState.getInt("num");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSaveInstanceState(Bundle outState) {
|
|
||||||
super.onSaveInstanceState(outState);
|
|
||||||
outState.putInt("num", mNum);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
Reference in New Issue
Block a user