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:
@@ -48,7 +48,7 @@
|
||||
to be installed if it doesn't exist. -->
|
||||
<uses-library android:name="com.example.will.never.exist" android:required="false" />
|
||||
|
||||
<activity android:name="ApiDemos" android:screenOrientation="landscape">
|
||||
<activity android:name="ApiDemos">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
|
||||
@@ -21,11 +21,11 @@
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
|
||||
<fragment android:name="com.example.android.apis.app.FragmentLayout$TitlesFragment"
|
||||
<fragment class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
|
||||
android:id="@+id/titles" android:layout_weight="1"
|
||||
android:layout_width="0px" android:layout_height="match_parent" />
|
||||
|
||||
<fragment android:name="com.example.android.apis.app.FragmentLayout$DialogFragment"
|
||||
<fragment class="com.example.android.apis.app.FragmentLayout$DialogFragment"
|
||||
android:id="@+id/dialog" android:layout_weight="1"
|
||||
android:layout_width="0px" android:layout_height="match_parent" />
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
android:orientation="horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<fragment android:name="com.example.android.apis.preference.FragmentPreferences$CategoriesFragment"
|
||||
<fragment class="com.example.android.apis.preference.FragmentPreferences$CategoriesFragment"
|
||||
android:id="@+id/categories"
|
||||
android:layout_width="0px"
|
||||
android:layout_height="match_parent"
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||
<fragment
|
||||
android:name="com.example.android.apis.app.FragmentLayout$TitlesFragment"
|
||||
class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
|
||||
android:id="@+id/titles"
|
||||
android:layout_width="match_parent" android:layout_height="match_parent" />
|
||||
</FrameLayout>
|
||||
|
||||
@@ -21,7 +21,7 @@
|
||||
android:orientation="vertical"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
<fragment android:name="com.example.android.apis.preference.FragmentPreferences$CategoriesFragment"
|
||||
<fragment class="com.example.android.apis.preference.FragmentPreferences$CategoriesFragment"
|
||||
android:id="@+id/categories"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0px"
|
||||
|
||||
@@ -58,6 +58,8 @@
|
||||
android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
|
||||
android:title="@string/title_fragment_preference"
|
||||
android:summary="@string/summary_fragment_preference">
|
||||
<!-- Arbitrary key/value pairs can be included for fragment arguments -->
|
||||
<extra android:name="someKey" android:value="somePrefValue" />
|
||||
</PreferenceScreen>
|
||||
|
||||
<!-- This PreferenceScreen tag sends the user to a completely different
|
||||
|
||||
@@ -16,16 +16,22 @@
|
||||
|
||||
<!-- This is a primitive example showing the different types of preferences available. -->
|
||||
<!-- BEGIN_INCLUDE(headers) -->
|
||||
<PreferenceHeaders
|
||||
<preference-headers
|
||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<Header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1Fragment"
|
||||
<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1Fragment"
|
||||
android:icon="@drawable/ic_settings_applications"
|
||||
android:title="Prefs 1"
|
||||
android:summary="An example of some preferences." />
|
||||
<Header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"
|
||||
|
||||
<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"
|
||||
android:icon="@drawable/ic_settings_display"
|
||||
android:title="Prefs 2"
|
||||
android:summary="Some other preferences you can see." />
|
||||
</PreferenceHeaders>
|
||||
android:summary="Some other preferences you can see.">
|
||||
<!-- Arbitrary key/value pairs can be included with a header as
|
||||
arguments to its fragment. -->
|
||||
<extra android:name="someKey" android:value="someHeaderValue" />
|
||||
</header>
|
||||
|
||||
</preference-headers>
|
||||
<!-- END_INCLUDE(headers) -->
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.example.android.apis.R;
|
||||
import android.os.Bundle;
|
||||
import android.preference.PreferenceActivity;
|
||||
import android.preference.PreferenceFragment;
|
||||
import android.util.Log;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -60,6 +61,9 @@ public class PreferenceWithHeaders extends PreferenceActivity {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Can retrieve arguments from preference XML.
|
||||
Log.i("args", "Arguments: " + getArguments());
|
||||
|
||||
// Load the preferences from an XML resource
|
||||
addPreferencesFromResource(R.xml.fragmented_preferences_inner);
|
||||
}
|
||||
@@ -73,6 +77,9 @@ public class PreferenceWithHeaders extends PreferenceActivity {
|
||||
public void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
// Can retrieve arguments from headers XML.
|
||||
Log.i("args", "Arguments: " + getArguments());
|
||||
|
||||
// Load the preferences from an XML resource
|
||||
addPreferencesFromResource(R.xml.preference_dependencies);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user