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. -->
|
to be installed if it doesn't exist. -->
|
||||||
<uses-library android:name="com.example.will.never.exist" android:required="false" />
|
<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>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
<category android:name="android.intent.category.DEFAULT" />
|
<category android:name="android.intent.category.DEFAULT" />
|
||||||
|
|||||||
@@ -21,11 +21,11 @@
|
|||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
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:id="@+id/titles" android:layout_weight="1"
|
||||||
android:layout_width="0px" android:layout_height="match_parent" />
|
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:id="@+id/dialog" android:layout_weight="1"
|
||||||
android:layout_width="0px" android:layout_height="match_parent" />
|
android:layout_width="0px" android:layout_height="match_parent" />
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="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:id="@+id/categories"
|
||||||
android:layout_width="0px"
|
android:layout_width="0px"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
|
|||||||
@@ -20,7 +20,7 @@
|
|||||||
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
android:layout_width="match_parent" android:layout_height="match_parent">
|
android:layout_width="match_parent" android:layout_height="match_parent">
|
||||||
<fragment
|
<fragment
|
||||||
android:name="com.example.android.apis.app.FragmentLayout$TitlesFragment"
|
class="com.example.android.apis.app.FragmentLayout$TitlesFragment"
|
||||||
android:id="@+id/titles"
|
android:id="@+id/titles"
|
||||||
android:layout_width="match_parent" android:layout_height="match_parent" />
|
android:layout_width="match_parent" android:layout_height="match_parent" />
|
||||||
</FrameLayout>
|
</FrameLayout>
|
||||||
|
|||||||
@@ -21,7 +21,7 @@
|
|||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="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:id="@+id/categories"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="0px"
|
android:layout_height="0px"
|
||||||
|
|||||||
@@ -58,6 +58,8 @@
|
|||||||
android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
|
android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
|
||||||
android:title="@string/title_fragment_preference"
|
android:title="@string/title_fragment_preference"
|
||||||
android:summary="@string/summary_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>
|
</PreferenceScreen>
|
||||||
|
|
||||||
<!-- This PreferenceScreen tag sends the user to a completely different
|
<!-- 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. -->
|
<!-- This is a primitive example showing the different types of preferences available. -->
|
||||||
<!-- BEGIN_INCLUDE(headers) -->
|
<!-- BEGIN_INCLUDE(headers) -->
|
||||||
<PreferenceHeaders
|
<preference-headers
|
||||||
xmlns:android="http://schemas.android.com/apk/res/android">
|
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:icon="@drawable/ic_settings_applications"
|
||||||
android:title="Prefs 1"
|
android:title="Prefs 1"
|
||||||
android:summary="An example of some preferences." />
|
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:icon="@drawable/ic_settings_display"
|
||||||
android:title="Prefs 2"
|
android:title="Prefs 2"
|
||||||
android:summary="Some other preferences you can see." />
|
android:summary="Some other preferences you can see.">
|
||||||
</PreferenceHeaders>
|
<!-- 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) -->
|
<!-- END_INCLUDE(headers) -->
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ import android.app.Dialog;
|
|||||||
import android.app.DialogFragment;
|
import android.app.DialogFragment;
|
||||||
import android.content.DialogInterface;
|
import android.content.DialogInterface;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
import android.view.View.OnClickListener;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
@@ -49,28 +50,49 @@ public class FragmentAlertDialog extends Activity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void showDialog() {
|
void showDialog() {
|
||||||
DialogFragment newFragment = new MyAlertDialogFragment();
|
DialogFragment newFragment = MyAlertDialogFragment.newInstance(
|
||||||
|
R.string.alert_dialog_two_buttons_title);
|
||||||
newFragment.show(this, "dialog");
|
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 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
|
@Override
|
||||||
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
public Dialog onCreateDialog(Bundle savedInstanceState) {
|
||||||
|
int title = getArguments().getInt("title");
|
||||||
|
|
||||||
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(title)
|
||||||
.setPositiveButton(R.string.alert_dialog_ok,
|
.setPositiveButton(R.string.alert_dialog_ok,
|
||||||
new DialogInterface.OnClickListener() {
|
new DialogInterface.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
public void onClick(DialogInterface dialog, int whichButton) {
|
||||||
/* User clicked OK so do some stuff */
|
((FragmentAlertDialog)getActivity()).doPositiveClick();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
.setNegativeButton(R.string.alert_dialog_cancel,
|
.setNegativeButton(R.string.alert_dialog_cancel,
|
||||||
new DialogInterface.OnClickListener() {
|
new DialogInterface.OnClickListener() {
|
||||||
public void onClick(DialogInterface dialog, int whichButton) {
|
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.
|
// 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();
|
getFragmentManager().openTransaction().add(android.R.id.content, content).commit();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class ContextMenuFragment extends Fragment {
|
public static class ContextMenuFragment extends Fragment {
|
||||||
|
|||||||
@@ -69,8 +69,8 @@ public class FragmentDialog extends Activity {
|
|||||||
// DialogFragment.show() will take care of adding the fragment
|
// DialogFragment.show() will take care of adding the fragment
|
||||||
// in a transaction. We also want to remove any currently showing
|
// in a transaction. We also want to remove any currently showing
|
||||||
// dialog, so make our own transaction and take care of that here.
|
// dialog, so make our own transaction and take care of that here.
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = getFragmentManager().openTransaction();
|
||||||
Fragment prev = findFragmentByTag("dialog");
|
Fragment prev = getFragmentManager().findFragmentByTag("dialog");
|
||||||
if (prev != null) {
|
if (prev != null) {
|
||||||
ft.remove(prev);
|
ft.remove(prev);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ 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.FragmentManager;
|
||||||
import android.app.FragmentTransaction;
|
import android.app.FragmentTransaction;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
@@ -35,13 +36,14 @@ public class FragmentMenu extends Activity {
|
|||||||
setContentView(R.layout.fragment_menu);
|
setContentView(R.layout.fragment_menu);
|
||||||
|
|
||||||
// Make sure the two menu fragments are created.
|
// Make sure the two menu fragments are created.
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentManager fm = getFragmentManager();
|
||||||
mFragment1 = findFragmentByTag("f1");
|
FragmentTransaction ft = fm.openTransaction();
|
||||||
|
mFragment1 = fm.findFragmentByTag("f1");
|
||||||
if (mFragment1 == null) {
|
if (mFragment1 == null) {
|
||||||
mFragment1 = new MenuFragment();
|
mFragment1 = new MenuFragment();
|
||||||
ft.add(mFragment1, "f1");
|
ft.add(mFragment1, "f1");
|
||||||
}
|
}
|
||||||
mFragment2 = findFragmentByTag("f2");
|
mFragment2 = fm.findFragmentByTag("f2");
|
||||||
if (mFragment2 == null) {
|
if (mFragment2 == null) {
|
||||||
mFragment2 = new Menu2Fragment();
|
mFragment2 = new Menu2Fragment();
|
||||||
ft.add(mFragment2, "f2");
|
ft.add(mFragment2, "f2");
|
||||||
@@ -67,7 +69,7 @@ public class FragmentMenu extends Activity {
|
|||||||
|
|
||||||
// Update fragment visibility based on current check box state.
|
// Update fragment visibility based on current check box state.
|
||||||
void updateFragmentVisibility() {
|
void updateFragmentVisibility() {
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = getFragmentManager().openTransaction();
|
||||||
if (mCheckBox1.isChecked()) ft.show(mFragment1);
|
if (mCheckBox1.isChecked()) ft.show(mFragment1);
|
||||||
else ft.hide(mFragment1);
|
else ft.hide(mFragment1);
|
||||||
if (mCheckBox2.isChecked()) ft.show(mFragment2);
|
if (mCheckBox2.isChecked()) ft.show(mFragment2);
|
||||||
@@ -90,8 +92,8 @@ public class FragmentMenu extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
||||||
menu.add("Menu 1a").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_ALWAYS);
|
menu.add("Menu 1b").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +110,7 @@ public class FragmentMenu extends Activity {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
|
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) {
|
if (savedInstanceState == null) {
|
||||||
// Do first time initialization -- add fragment.
|
// Do first time initialization -- add fragment.
|
||||||
Fragment newFragment = new ReceiveResultFragment();
|
Fragment newFragment = new ReceiveResultFragment();
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = getFragmentManager().openTransaction();
|
||||||
ft.add(R.id.simple_fragment, newFragment).commit();
|
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.Activity;
|
||||||
import android.app.Fragment;
|
import android.app.Fragment;
|
||||||
|
import android.app.FragmentManager;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
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.
|
// 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 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();
|
fm.openTransaction().add(mRetainedFragment, "retained").commit();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,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 = CountingFragment.newInstance(mStackLevel);
|
Fragment newFragment = CountingFragment.newInstance(mStackLevel);
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = getFragmentManager().openTransaction();
|
||||||
ft.add(R.id.simple_fragment, newFragment).commit();
|
ft.add(R.id.simple_fragment, newFragment).commit();
|
||||||
} else {
|
} else {
|
||||||
mStackLevel = savedInstanceState.getInt("level");
|
mStackLevel = savedInstanceState.getInt("level");
|
||||||
@@ -69,7 +69,7 @@ public class FragmentStack extends Activity {
|
|||||||
|
|
||||||
// Add the fragment to the activity, pushing this transaction
|
// Add the fragment to the activity, pushing this transaction
|
||||||
// on to the back stack.
|
// on to the back stack.
|
||||||
FragmentTransaction ft = openFragmentTransaction();
|
FragmentTransaction ft = getFragmentManager().openTransaction();
|
||||||
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);
|
||||||
ft.addToBackStack(null);
|
ft.addToBackStack(null);
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import com.example.android.apis.R;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceActivity;
|
import android.preference.PreferenceActivity;
|
||||||
import android.preference.PreferenceFragment;
|
import android.preference.PreferenceFragment;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@@ -60,6 +61,9 @@ public class PreferenceWithHeaders extends PreferenceActivity {
|
|||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
// Can retrieve arguments from preference XML.
|
||||||
|
Log.i("args", "Arguments: " + getArguments());
|
||||||
|
|
||||||
// Load the preferences from an XML resource
|
// Load the preferences from an XML resource
|
||||||
addPreferencesFromResource(R.xml.fragmented_preferences_inner);
|
addPreferencesFromResource(R.xml.fragmented_preferences_inner);
|
||||||
}
|
}
|
||||||
@@ -73,6 +77,9 @@ public class PreferenceWithHeaders extends PreferenceActivity {
|
|||||||
public void onCreate(Bundle savedInstanceState) {
|
public void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
|
||||||
|
// Can retrieve arguments from headers XML.
|
||||||
|
Log.i("args", "Arguments: " + getArguments());
|
||||||
|
|
||||||
// Load the preferences from an XML resource
|
// Load the preferences from an XML resource
|
||||||
addPreferencesFromResource(R.xml.preference_dependencies);
|
addPreferencesFromResource(R.xml.preference_dependencies);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user