public abstract class PreferenceActivity
extends ListActivity implements PreferenceFragment.OnPreferenceStartFragmentCallback
| java.lang.Object | ||||||
| android.content.Context | ||||||
| android.content.ContextWrapper | ||||||
| android.view.ContextThemeWrapper | ||||||
| android.app.Activity | ||||||
| android.app.ListActivity | ||||||
| android.preference.PreferenceActivity | ||||||
这是向用户显示偏好层次结构的活动的基类。 在HONEYCOMB之前,这门课只允许显示一组偏好; 这个功能现在应该在新的PreferenceFragment类中找到。 如果您在旧模式下使用PreferenceActivity,则其中的文档适用于此处不赞成使用的API。
此活动显示一个或多个首选项,每个首选项都与PreferenceFragment关联以显示该首部的首选项。 然而,这些关联的实际布局和显示可能会有所不同; 目前有两种主要方法可能采取:
PreferenceActivity的子类应该实现onBuildHeaders(List
以使用所需的项目填充标题列表。 这样做隐式地将类切换为新的“标题+片段”模式,而不是仅显示单个首选项列表的旧样式。
有关使用 PreferenceActivity信息,请阅读 Settings指南。
以下示例代码显示了一个简单的首选项活动,它具有两组不同的首选项。 由活动本身及其两个偏好片段组成的实现是:
public class PreferenceWithHeaders extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add a button to the header list.
if (hasHeaders()) {
Button button = new Button(this);
button.setText("Some action");
setListFooter(button);
}
}
/**
* Populate the activity with the top-level headers.
*/
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.preference_headers, target);
}
/**
* This fragment shows the preferences for the first header.
*/
public static class Prefs1Fragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Make sure default values are applied. In a real app, you would
// want this in a shared function that is used to retrieve the
// SharedPreferences wherever they are needed.
PreferenceManager.setDefaultValues(getActivity(),
R.xml.advanced_preferences, false);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.fragmented_preferences);
}
}
/**
* This fragment contains a second-level set of preference that you
* can get to by tapping an item in the first preferences fragment.
*/
public static class Prefs1FragmentInner extends PreferenceFragment {
@Override
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);
}
}
/**
* This fragment shows the preferences for the second header.
*/
public static class Prefs2Fragment extends PreferenceFragment {
@Override
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);
}
}
}
preference_headers资源描述了要显示的标题以及与它们关联的片段。 它是:
<preference-headers
xmlns:android="http://schemas.android.com/apk/res/android">
<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"
android:icon="@drawable/ic_settings_display"
android:title="Prefs 2"
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>
<header android:icon="@drawable/ic_settings_display"
android:title="Intent"
android:summary="Launches an Intent.">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" />
</header>
</preference-headers>
第一个标题由Prefs1Fragment显示,它由以下XML资源填充:
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/inline_preferences">
<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/title_checkbox_preference"
android:summary="@string/summary_checkbox_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/dialog_based_preferences">
<EditTextPreference
android:key="edittext_preference"
android:title="@string/title_edittext_preference"
android:summary="@string/summary_edittext_preference"
android:dialogTitle="@string/dialog_title_edittext_preference" />
<ListPreference
android:key="list_preference"
android:title="@string/title_list_preference"
android:summary="@string/summary_list_preference"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="@string/dialog_title_list_preference" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/launch_preferences">
<!-- This PreferenceScreen tag sends the user to a new fragment of
preferences. If running in a large screen, they can be embedded
inside of the overall preferences UI. -->
<PreferenceScreen
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
activity, switching out of the current preferences UI. -->
<PreferenceScreen
android:title="@string/title_intent_preference"
android:summary="@string/summary_intent_preference">
<intent android:action="android.intent.action.VIEW"
android:data="http://www.android.com" />
</PreferenceScreen>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/preference_attributes">
<CheckBoxPreference
android:key="parent_checkbox_preference"
android:title="@string/title_parent_preference"
android:summary="@string/summary_parent_preference" />
<!-- The visual style of a child is defined by this styled theme attribute. -->
<CheckBoxPreference
android:key="child_checkbox_preference"
android:dependency="parent_checkbox_preference"
android:layout="?android:attr/preferenceLayoutChild"
android:title="@string/title_child_preference"
android:summary="@string/summary_child_preference" />
</PreferenceCategory>
</PreferenceScreen>
请注意,此XML资源包含一个保存另一个片段的首选项屏幕,此处实现了Prefs1FragmentInner。 这允许用户遍历偏好的层次结构; 按下后会弹出堆栈中的每个片段以返回到先前的首选项。
有关实现片段本身的信息,请参见 PreferenceFragment 。
Nested classes |
|
|---|---|
class |
PreferenceActivity.Header 用户可以选择的单个标题项目的描述。 |
常量(Constants) |
|
|---|---|
String |
EXTRA_NO_HEADERS 当开始这个活动时,调用的Intent可以包含这个额外的布尔值,不应该显示标题列表。 |
String |
EXTRA_SHOW_FRAGMENT 当开始这个活动时,调用的Intent可以包含这个额外的字符串来指定最初应该显示哪个片段。 |
String |
EXTRA_SHOW_FRAGMENT_ARGUMENTS 在启动此活动并使用 |
String |
EXTRA_SHOW_FRAGMENT_SHORT_TITLE 在开始此活动并使用 |
String |
EXTRA_SHOW_FRAGMENT_TITLE 当开始此活动并使用 |
long |
HEADER_ID_UNDEFINED
|
Inherited constants |
|---|
android.app.Activity
|
android.content.Context
|
android.content.ComponentCallbacks2
|
Inherited fields |
|---|
android.app.Activity
|
Public constructors |
|
|---|---|
PreferenceActivity() |
|
公共方法(Public methods) |
|
|---|---|
void |
addPreferencesFromIntent(Intent intent) 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
void |
addPreferencesFromResource(int preferencesResId) 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
Preference |
findPreference(CharSequence key) 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
void |
finishPreferencePanel(Fragment caller, int resultCode, Intent resultData) 由首选项面板片段调用以完成其自身。 |
PreferenceManager |
getPreferenceManager() 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
PreferenceScreen |
getPreferenceScreen() 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
boolean |
hasHeaders() 如果此活动当前正在显示标题列表,则返回true。 |
void |
invalidateHeaders() 当您需要更改正在显示的标题时呼叫。 |
boolean |
isMultiPane() 如果此活动显示多个窗格 - 标题和首选项片段,则返回true。 |
void |
loadHeadersFromResource(int resid, List<PreferenceActivity.Header> target) 将给定的XML文件解析为标题描述,将每个已解析的标题添加到目标列表中。 |
void |
onBuildHeaders(List<PreferenceActivity.Header> target) 当活动需要构建标题列表时调用。 |
Intent |
onBuildStartFragmentIntent(String fragmentName, Bundle args, int titleRes, int shortTitleRes) 在单窗格模式下由 |
void |
onContentChanged() 内容更改时更新屏幕状态(当前列表和其他视图)。 |
PreferenceActivity.Header |
onGetInitialHeader() 调用以确定要显示的初始标题。 |
PreferenceActivity.Header |
onGetNewHeader() 在标题列表更新后调用( |
void |
onHeaderClick(PreferenceActivity.Header header, int position) 当用户在标题列表中选择一个项目时调用。 |
boolean |
onIsHidingHeaders() 调用以确定是否应该隐藏标题列表。 |
boolean |
onIsMultiPane() 调用以确定活动是否应以多窗格模式运行。 |
boolean |
onPreferenceStartFragment(PreferenceFragment caller, Preference pref) 当用户点击一个具有与其关联的片段类名的首选项时调用。 |
boolean |
onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
void |
setListFooter(View view) 设置应显示在标题列表底部的页脚。 |
void |
setParentTitle(CharSequence title, CharSequence shortTitle, View.OnClickListener listener) 应在onCreate之后调用以确保创建面包屑(如果有的话)。 |
void |
setPreferenceScreen(PreferenceScreen preferenceScreen) 此方法在API级别11中已弃用。此函数与现代基于片段的PreferenceActivity无关。 |
void |
showBreadCrumbs(CharSequence title, CharSequence shortTitle) 更改当前首选项的面包屑的基本标题。 |
void |
startPreferenceFragment(Fragment fragment, boolean push) 开始一个新的片段。 |
void |
startPreferencePanel(String fragmentClass, Bundle args, int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode) 开始一个包含首选项面板的新片段。 |
void |
startWithFragment(String fragmentName, Bundle args, Fragment resultTo, int resultRequestCode, int titleRes, int shortTitleRes) 开始此活动的新实例,仅显示给定的首选项片段。 |
void |
startWithFragment(String fragmentName, Bundle args, Fragment resultTo, int resultRequestCode) 像 |
void |
switchToHeader(String fragmentName, Bundle args) 在双窗格模式下时,切换片段窗格以显示给定的首选项片段。 |
void |
switchToHeader(PreferenceActivity.Header header) 在双窗格模式下,切换到片段窗格以显示给定的首选项片段。 |
Protected methods |
|
|---|---|
boolean |
isValidFragment(String fragmentName) 子类应该重写此方法,并验证给定片段是否附加到此活动的有效类型。 |
void |
onActivityResult(int requestCode, int resultCode, Intent data) 当您启动的活动退出时调用,为您提供您启动的requestCode,返回的resultCode以及其中的任何其他数据。 |
void |
onCreate(Bundle savedInstanceState) 当活动开始时调用。 |
void |
onDestroy() 在活动销毁之前执行任何最终清理。 |
void |
onListItemClick(ListView l, View v, int position, long id) 当选择列表中的项目时,将调用此方法。 |
void |
onNewIntent(Intent intent) 这是针对在其软件包中将launchMode设置为“singleTop”的活动,或者如果客户在拨打 |
void |
onRestoreInstanceState(Bundle state) 确保在活动恢复所有视图状态之前创建了列表视图。 |
void |
onSaveInstanceState(Bundle outState) 在被杀死之前调用以从活动中检索每个实例的状态,以便可以在 |
void |
onStop() 当用户不再可见时调用。 |
继承方法(Inherited methods) |
|
|---|---|
android.app.ListActivity
|
|
android.app.Activity
|
|
android.view.ContextThemeWrapper
|
|
android.content.ContextWrapper
|
|
android.content.Context
|
|
java.lang.Object
|
|
android.view.LayoutInflater.Factory2
|
|
android.view.Window.Callback
|
|
android.view.KeyEvent.Callback
|
|
android.view.View.OnCreateContextMenuListener
|
|
android.content.ComponentCallbacks2
|
|
android.preference.PreferenceFragment.OnPreferenceStartFragmentCallback
|
|
android.view.LayoutInflater.Factory
|
|
android.content.ComponentCallbacks
|
|
String EXTRA_NO_HEADERS
当开始这个活动时,调用的Intent可以包含这个额外的布尔值,不应该显示标题列表。 这通常与EXTRA_SHOW_FRAGMENT结合使用来启动该活动以显示用户已导航到的特定片段。
常量值:“:android:no_headers”
String EXTRA_SHOW_FRAGMENT
当开始这个活动时,调用的Intent可以包含这个额外的字符串来指定最初应该显示哪个片段。
Starting from Key Lime Pie, when this argument is passed in, the PreferenceActivity will call isValidFragment() to confirm that the fragment class name is valid for this activity.常量值:“:android:show_fragment”
String EXTRA_SHOW_FRAGMENT_ARGUMENTS
当启动此活动并使用 EXTRA_SHOW_FRAGMENT ,还可以指定此额外参数,以便在PreferenceActivity的初始创建过程中实例化时提供一个参数的Bundle以传递给该片段。
常量值:“:android:show_fragment_args”
String EXTRA_SHOW_FRAGMENT_SHORT_TITLE
在开始此活动并使用 EXTRA_SHOW_FRAGMENT ,还可以指定此附加内容以提供为该片段显示的简短标题。
常量值:“:android:show_fragment_short_title”
String EXTRA_SHOW_FRAGMENT_TITLE
当开始此活动并使用 EXTRA_SHOW_FRAGMENT ,还可以指定此额外 EXTRA_SHOW_FRAGMENT以提供要为该片段显示的标题。
常量值:“:android:show_fragment_title”
long HEADER_ID_UNDEFINED
Header.id默认值表示未设置标识符值。 所有其他值(包括低于-1的值)都是有效的。
常量值:-1(0xffffffffffffffff)
void addPreferencesFromIntent (Intent intent)
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
添加与给定 Intent匹配的活动的首选项。
| 参数(Parameters) | |
|---|---|
intent |
Intent: The Intent to query activities. |
void addPreferencesFromResource (int preferencesResId)
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
使给定的XML资源膨胀并将偏好层次结构添加到当前偏好层次结构中。
| 参数(Parameters) | |
|---|---|
preferencesResId |
int: The XML resource ID to inflate. |
Preference findPreference (CharSequence key)
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
根据其密钥找到 Preference 。
| 参数(Parameters) | |
|---|---|
key |
CharSequence: The key of the preference to retrieve. |
| 返回(Returns) | |
|---|---|
Preference |
The Preference with the key, or null. |
也可以看看:
void finishPreferencePanel (Fragment caller, int resultCode, Intent resultData)
由首选项面板片段调用以完成其自身。
| 参数(Parameters) | |
|---|---|
caller |
Fragment: The fragment that is asking to be finished. |
resultCode |
int: Optional result code to send back to the original launching fragment. |
resultData |
Intent: Optional result data to send back to the original launching fragment. |
PreferenceManager getPreferenceManager ()
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
返回此活动使用的 PreferenceManager 。
| 返回(Returns) | |
|---|---|
PreferenceManager |
The PreferenceManager. |
PreferenceScreen getPreferenceScreen ()
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
获取此活动显示的首选项层次的根。
| 返回(Returns) | |
|---|---|
PreferenceScreen |
The PreferenceScreen that is the root of the preference hierarchy. |
void invalidateHeaders ()
当您需要更改正在显示的标题时呼叫。 稍后将调用onBuildHeaders()来检索新列表。
boolean isMultiPane ()
如果此活动显示多个窗格 - 标题和首选项片段,则返回true。
| 返回(Returns) | |
|---|---|
boolean |
|
void loadHeadersFromResource (int resid,
List<PreferenceActivity.Header> target)
将给定的XML文件解析为标题描述,将每个已解析的标题添加到目标列表中。
| 参数(Parameters) | |
|---|---|
resid |
int: The XML resource to load and parse. |
target |
List: The list in which the parsed headers should be placed. |
void onBuildHeaders (List<PreferenceActivity.Header> target)
当活动需要构建标题列表时调用。 通过执行此操作并将至少一个项目添加到列表中,您将使活动以其现代片段模式运行。 请注意,此功能可能并不总是被调用; 例如,如果活动被要求显示没有标题列表的特定片段,则不需要构建标题。
典型的实现将使用 loadHeadersFromResource(int, List
来从资源填充列表。
| 参数(Parameters) | |
|---|---|
target |
List: The list in which to place the headers. |
Intent onBuildStartFragmentIntent (String fragmentName, Bundle args, int titleRes, int shortTitleRes)
在单窗格模式下由startWithFragment(String, Bundle, Fragment, int, int, int)调用时,要构建一个意图以启动显示所选片段的新活动。 默认实现构造一个Intent,它用适当的参数重新启动当前活动以显示片段。
| 参数(Parameters) | |
|---|---|
fragmentName |
String: The name of the fragment to display. |
args |
Bundle: Optional arguments to supply to the fragment. |
titleRes |
int: Optional resource ID of title to show for this item. |
shortTitleRes |
int: Optional resource ID of short title to show for this item. |
| 返回(Returns) | |
|---|---|
Intent |
Returns an Intent that can be launched to display the given fragment. |
PreferenceActivity.Header onGetInitialHeader ()
调用以确定要显示的初始标题。 默认实现只是返回第一个头的片段。 请注意,返回的Header对象实际上并不需要存在于您的头文件列表中 - 无论它的片段是什么,它都将仅用于显示初始UI。
| 返回(Returns) | |
|---|---|
PreferenceActivity.Header |
|
PreferenceActivity.Header onGetNewHeader ()
在标题列表更新后调用( onBuildHeaders(List
已被调用并返回到invalidateHeaders() )以指定现在应该选择的标题。 默认实现返回null以保持当前选择的标题。
| 返回(Returns) | |
|---|---|
PreferenceActivity.Header |
|
void onHeaderClick (PreferenceActivity.Header header, int position)
当用户在标题列表中选择一个项目时调用。 默认实现将根据需要调用startWithFragment(String, Bundle, Fragment, int, int, int)或switchToHeader(Header) 。
| 参数(Parameters) | |
|---|---|
header |
PreferenceActivity.Header: The header that was selected. |
position |
int: The header's position in the list. |
boolean onIsHidingHeaders ()
调用以确定是否应该隐藏标题列表。 默认实现返回EXTRA_NO_HEADERS给出的值,如果未提供,则返回false。 例如,当该活动正在重新启动以显示特定的偏好活动时,它将被设置为false。
| 返回(Returns) | |
|---|---|
boolean |
|
boolean onIsMultiPane ()
调用以确定活动是否应以多窗格模式运行。 如果屏幕足够大,则默认实现返回true。
| 返回(Returns) | |
|---|---|
boolean |
|
boolean onPreferenceStartFragment (PreferenceFragment caller, Preference pref)
当用户点击一个具有与其关联的片段类名的首选项时调用。 实现应该实例化并切换到给定片段的实例。
| 参数(Parameters) | |
|---|---|
caller |
PreferenceFragment
|
pref |
Preference
|
| 返回(Returns) | |
|---|---|
boolean |
|
boolean onPreferenceTreeClick (PreferenceScreen preferenceScreen, Preference preference)
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
| 参数(Parameters) | |
|---|---|
preferenceScreen |
PreferenceScreen
|
preference |
Preference
|
| 返回(Returns) | |
|---|---|
boolean |
|
void setListFooter (View view)
设置应显示在标题列表底部的页脚。
| 参数(Parameters) | |
|---|---|
view |
View
|
void setParentTitle (CharSequence title, CharSequence shortTitle, View.OnClickListener listener)
应在onCreate之后调用以确保创建面包屑(如果有的话)。 这会为片段面包屑添加一个标题,并将监听器附加到父项上的任何点击。
| 参数(Parameters) | |
|---|---|
title |
CharSequence: the title for the breadcrumb |
shortTitle |
CharSequence: the short title for the breadcrumb |
listener |
View.OnClickListener
|
void setPreferenceScreen (PreferenceScreen preferenceScreen)
此方法在API级别11中已弃用。
此功能与现代基于片段的PreferenceActivity无关。
设置此活动显示的偏好层次的根。
| 参数(Parameters) | |
|---|---|
preferenceScreen |
PreferenceScreen: The root PreferenceScreen of the preference hierarchy. |
void showBreadCrumbs (CharSequence title, CharSequence shortTitle)
更改当前首选项的面包屑的基本标题。 这通常会要求你。 有关更多信息,请参阅FragmentBreadCrumbs 。
| 参数(Parameters) | |
|---|---|
title |
CharSequence
|
shortTitle |
CharSequence
|
void startPreferenceFragment (Fragment fragment, boolean push)
开始一个新的片段。
| 参数(Parameters) | |
|---|---|
fragment |
Fragment: The fragment to start |
push |
boolean: If true, the current fragment will be pushed onto the back stack. If false, the current fragment will be replaced. |
void startPreferencePanel (String fragmentClass, Bundle args, int titleRes, CharSequence titleText, Fragment resultTo, int resultRequestCode)
开始一个包含首选项面板的新片段。 如果首选项以多窗格模式显示,则给定的片段类将被实例化并放置在适当的窗格中。 如果以单窗格模式运行,将启动一个新的活动以显示片段。
| 参数(Parameters) | |
|---|---|
fragmentClass |
String: Full name of the class implementing the fragment. |
args |
Bundle: Any desired arguments to supply to the fragment. |
titleRes |
int: Optional resource identifier of the title of this fragment. |
titleText |
CharSequence: Optional text of the title of this fragment. |
resultTo |
Fragment: Optional fragment that result data should be sent to. If non-null, resultTo.onActivityResult() will be called when this preference panel is done. The launched panel must use finishPreferencePanel(Fragment, int, Intent) when done. |
resultRequestCode |
int: If resultTo is non-null, this is the caller's request code to be received with the result. |
void startWithFragment (String fragmentName, Bundle args, Fragment resultTo, int resultRequestCode, int titleRes, int shortTitleRes)
开始此活动的新实例,仅显示给定的首选项片段。 在此模式下启动时,标题列表将被隐藏,给定的首选项片段将被实例化并填充整个活动。
| 参数(Parameters) | |
|---|---|
fragmentName |
String: The name of the fragment to display. |
args |
Bundle: Optional arguments to supply to the fragment. |
resultTo |
Fragment: Option fragment that should receive the result of the activity launch. |
resultRequestCode |
int: If resultTo is non-null, this is the request code in which to report the result. |
titleRes |
int: Resource ID of string to display for the title of this set of preferences. |
shortTitleRes |
int: Resource ID of string to display for the short title of this set of preferences. |
void startWithFragment (String fragmentName, Bundle args, Fragment resultTo, int resultRequestCode)
像 startWithFragment(String, Bundle, Fragment, int, int, int)但使用0 titleRes。
| 参数(Parameters) | |
|---|---|
fragmentName |
String
|
args |
Bundle
|
resultTo |
Fragment
|
resultRequestCode |
int
|
void switchToHeader (String fragmentName, Bundle args)
在双窗格模式下时,切换片段窗格以显示给定的首选项片段。
| 参数(Parameters) | |
|---|---|
fragmentName |
String: The name of the fragment to display. |
args |
Bundle: Optional arguments to supply to the fragment. |
void switchToHeader (PreferenceActivity.Header header)
在双窗格模式下,切换到片段窗格以显示给定的首选项片段。
| 参数(Parameters) | |
|---|---|
header |
PreferenceActivity.Header: The new header to display. |
boolean isValidFragment (String fragmentName)
子类应该重写此方法,并验证给定片段是否附加到此活动的有效类型。 默认实现返回true了专为应用android:targetSdkVersion比老KITKAT 。 对于更高版本,它会抛出异常。
| 参数(Parameters) | |
|---|---|
fragmentName |
String: the class name of the Fragment about to be attached to this activity. |
| 返回(Returns) | |
|---|---|
boolean |
true if the fragment class name is valid for this Activity and false otherwise. |
void onActivityResult (int requestCode,
int resultCode,
Intent data)
当您启动的活动退出时调用,为您提供您启动的requestCode,返回的resultCode以及其中的任何其他数据。 如果活动显式返回该结果,未返回任何结果或在其操作期间崩溃, resultCode将为RESULT_CANCELED 。
当您的活动重新开始时,您将在onResume()之前立即收到此电话。
如果你的活动设置,该方法不会调用 noHistory至 true 。
| 参数(Parameters) | |
|---|---|
requestCode |
int: The integer request code originally supplied to startActivityForResult(), allowing you to identify who this result came from. |
resultCode |
int: The integer result code returned by the child activity through its setResult(). |
data |
Intent: An Intent, which can return result data to the caller (various data can be attached to Intent "extras"). |
void onCreate (Bundle savedInstanceState)
当活动开始时调用。 这是大多数初始化应该去的地方:调用setContentView(int)来setContentView(int)活动的UI,使用findViewById(int)以编程方式与UI中的小部件进行交互,调用managedQuery(android.net.Uri, String[], String, String[], String)以检索显示的数据的光标等。
您可以拨打 finish()从这个函数中,在这种情况下的onDestroy()将被立即调用没有任何活动的生命周期(其余的 onStart() , onResume() , onPause()执行等)。
派生类必须调用超类的这个方法的实现。 如果他们不这样做,就会抛出异常。
| 参数(Parameters) | |
|---|---|
savedInstanceState |
Bundle: If the activity is being re-initialized after previously being shut down then this Bundle contains the data it most recently supplied in onSaveInstanceState(Bundle). Note: Otherwise it is null. |
void onDestroy ()
在活动销毁之前执行任何最终清理。 这可能是由于活动正在完成(有人称为finish() ,或者系统暂时销毁此活动的实例以节省空间),您可以使用isFinishing()方法区分这两种情况。
注意:不要将此方法称为保存数据的地方! 例如,如果一个活动正在编辑内容提供者中的数据,那么这些编辑应该在onPause()或onSaveInstanceState(Bundle) ,而不是在这里onSaveInstanceState(Bundle) 。 这种方法通常被实现为释放资源,例如与活动相关联的线程,这样一个被销毁的活动就不会离开这些东西,而其他应用程序仍在运行。 在某些情况下,系统只会在不调用该方法(或任何其他方法)的情况下终止该活动的托管过程,因此不应该将其用于在过程消失后执行那些旨在保留的事情。
派生类必须调用超类的这个方法的实现。 如果他们不这样做,就会抛出异常。
void onListItemClick (ListView l, View v, int position, long id)
当选择列表中的项目时,将调用此方法。 子类应该重写。 如果子类需要访问与选定项相关的数据,它们可以调用getListView()。getItemAtPosition(position)。
| 参数(Parameters) | |
|---|---|
l |
ListView: The ListView where the click happened |
v |
View: The view that was clicked within the ListView |
position |
int: The position of the view in the list |
id |
long: The row id of the item that was clicked |
void onNewIntent (Intent intent)
这是针对在其包中将launchMode设置为“singleTop”的活动,或者如果客户在拨打startActivity(Intent)时使用了FLAG_ACTIVITY_SINGLE_TOP标志。 在这两种情况下,当活动在活动堆栈顶部重新启动时,而不是启动活动的新实例时,将使用用于重新启动的Intent在现有实例上调用onNewIntent()它。
在接收新的意图之前,活动总是会暂停,因此您可以依靠在此方法后调用 onResume() 。
请注意, getIntent()仍会返回原始意图。 您可以使用setIntent(Intent)将其更新为新的Intent。
| 参数(Parameters) | |
|---|---|
intent |
Intent: The new intent that was started for the activity. |
void onRestoreInstanceState (Bundle state)
确保在活动恢复所有视图状态之前创建了列表视图。
| 参数(Parameters) | |
|---|---|
state |
Bundle: the data most recently supplied in onSaveInstanceState(Bundle). |
void onSaveInstanceState (Bundle outState)
被调用以在被杀死之前从活动检索每个实例状态,以便可以在 onCreate(Bundle)或 onRestoreInstanceState(Bundle)恢复状态(由此方法填充的 Bundle将被传递给两者)。
在一个活动可能被杀死之前调用这个方法,以便在将来它有一段时间它可以恢复它的状态。 例如,如果活动B在活动A之前启动,并且在某些时刻活动A被杀死以回收资源,则活动A将有机会通过此方法保存其用户界面的当前状态,以便当用户返回时到活动A,用户界面的状态可以通过onCreate(Bundle)或onRestoreInstanceState(Bundle)来恢复。
不要将此方法与活动生命周期回调(如onPause() ,后者始终在活动置于后台或销毁途中调用,或在销毁前调用onStop() 。 调用onPause()和onStop()一个示例是,而不是此方法是用户从活动B返回到活动A时:不需要在B上调用onSaveInstanceState(Bundle) ,因为该特定实例永远不会被恢复,因此系统可避免调用它。 onPause()被调用时的一个示例,而不是onSaveInstanceState(Bundle)是活动B在活动A前面启动时的示例A:如果活动A在A的用户界面的状态期间未在B的生命周期中被onSaveInstanceState(Bundle) ,则可以避免在活动A上调用onSaveInstanceState(Bundle)将保持完好。
默认实现通过在具有ID的层次结构中的每个视图上调用onSaveInstanceState() ,并通过保存当前焦点视图的ID(所有视图都由缺省实现恢复)来为您处理大部分UI每个实例状态onRestoreInstanceState(Bundle) )。 如果您重写此方法以保存未被每个视图捕获的附加信息,那么您可能需要调用默认实现,否则应准备好保存每个视图的所有状态。
如果调用,则此方法将在onStop()之前onStop() 。 onPause()之前或之后是否会发生,并不能保证。
| 参数(Parameters) | |
|---|---|
outState |
Bundle: Bundle in which to place your saved state. |
void onStop ()
当用户不再可见时调用。 接下来,您将收到无论是onRestart() , onDestroy() ,或没有,这取决于后来的用户活动。
派生类必须调用超类的这个方法的实现。 如果他们不这样做,就会抛出异常。