2013-02-20 83 views
0

Preference类有一个名为getExtras()的方法。
它可能或可能与偏好意图无关,但可以直接使用意图获取和放置附加内容。
Preference类中没有方法putExtra/s(),那么...
getExtras()的目的是什么?它使用哪种场景?Android什么是Preference.getExtras()用于?

回答

5

getExtras()的目的是什么?

它并没有真正做任何有用的事情。认真。

the Preference source code,有一个private成员变量mExtras

private Bundle mExtras; 

然而,它永远不会以任何方式改变(并且不能由外部类任何被访问),除了在下面:

public Bundle getExtras() { 
    if (mExtras == null) { 
     mExtras = new Bundle(); 
    } 
    return mExtras; 
} 
public Bundle peekExtras() { 
    return mExtras; 
} 

我想它可能会用于未来的某些事情,但它被添加到API 11中并且通过API 16保持无用。

Preference类中没有方法putExtra/s(),那么...在哪个场景中使用?

我想你可以使用它与项目偏好关联,如:

Bundle extras = myPref.getExtras(); 
extras.putString("KEY", "Value"); 

你不需要putExtra()这样做,而不是直接访问Bundle。但这似乎是所有这些都很有用。

0

在文档中没有很好地解释它,但#getExtras适用于通过#setFragment开始另一个片段的首选项。只要您使用PreferenceActivity,就会在首选项are passed to the specified fragment上指定任何附加项。