這是對this question的補充。啓動活動,並得到結果onActivityResult
我可以開始練習,但我也需要能夠得到的結果。我該怎麼做?
我想對我的PreferencesActivity無濟於事覆蓋onActivityResult。
我是否缺少preferences.xml中的一些額外屬性?
這是對this question的補充。啓動活動,並得到結果onActivityResult
我可以開始練習,但我也需要能夠得到的結果。我該怎麼做?
我想對我的PreferencesActivity無濟於事覆蓋onActivityResult。
我是否缺少preferences.xml中的一些額外屬性?
如果你想從偏好活動數據傳遞到您的主要活動中使用此代碼:
在您的主要活動類(發射):
startActivityForResult(new Intent(main.this, preferences.class), 0);
在你的偏好活動類(設置結果):
Intent i;
i.putStringExtra("name", "tom");
setResult(RESULT_OK, i);
finish();
在主活動類(得到的結果):
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0) {
if (resultCode == RESULT_OK){
Log.d("test", data.getExtraString("name");
}
}
}
你可以把儘可能多的羣衆演員,只要你想不僅字符串,但所有標準數據類型。
希望我做的一切權利;)
編輯
由於Kaarel告訴我,我可能missunderstood問題。 這就是你可以從喜好的活動主要activiy收到數據:
在您的主要活動:啓動喜好的活動和附加數據
String foo = "hello";
Intent i = new Intent();
i.putExtra("testString", foo);//You can also add other types of variables here, see [1] for reference
i.setClass(main.this, preferences.class);
startActivity(i);
在你喜好的活動:收到附連到意圖
Bundle b = this.getIntent().getExtras();//[2]
if (b!=null){
String recievedString = b.getString("testString");
//The recievedString variable contains the String "hello" now, see [3]
}
[1] https://developer.android.com/reference/android/content/Intent.html
的數據[2] https://developer.android.com/reference/android/content/Intent.html#getExtras%28%29
[3] https://developer.android.com/reference/android/os/Bundle.html
您不回答OP詢問的問題。問題是:如何處理從首選項活動啓動的意圖的結果,即「onActivityResult」應該位於「PreferencesActivity」中。 – Kaarel 2012-12-30 19:30:06
我認爲你是對的我會編輯答案 – MazeChaZer 2012-12-30 20:10:07
我想,你仍然在回答錯誤的問題...... :( – Kaarel 2012-12-31 10:31:01
,我知道的是,聽取他們對偏好的點擊,並明確啓動意圖乾淨的解決方案。這種方式onActivityResult
將照常進行。
假設你的意圖,喜好以XML定義,你可以將監聽器附加給它這樣的(其中1234
是onActivityResult
請求代碼):
Preference pref = (Preference) findPreference("prefKey");
pref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
startActivityForResult(preference.getIntent(), 1234);
return true;
}
});
嘗試在你的PreferencesActivity類中重寫startActivity()
並它叫startActivityForResult()
檢查的意圖是一個我們感興趣的是,類似如下(以1234爲請求代碼)之後,而不是:
public class PreferencesActivity {
// ...
@Override
public void startActivity(Intent intent) {
if (thisIsTheExpected(intent)) {
super.startActivityForResult(intent, 1234);
} else {
super.startActivity(intent);
}
}
@Override
protected void onActivityResult(int reqCode, int resCode, Intent data) {
if (reqCode == 1234) {
// should be getting called now
}
}
// ...
}
根據您的需求,相比這可能是簡單的增加幾個OnPreferenceClickListener
在你的代碼:)
不錯的主意,雖然我擔心它可能會中斷(在Android的未來版本中),因爲它假定當單擊首選項時始終調用「startActivity(Intent)」。此外,thisIsTheExpected(intent)似乎不那麼直接因此比匹配偏好鍵更容易出錯,所以暫時我會用我的解決方案。 – Kaarel 2013-01-06 09:05:14
如果你在1000線看看PreferenceActivity.java在平臺源代碼here你可以看到你的意圖是通過startActivity startActivity(header.intent);
稱爲而不是通過startActivityForResult,所以我不認爲這是可能的。
但是,您可以嘗試覆蓋PreferenceActivity的onHeaderClick
函數以及onActivityResult
並查看會發生什麼情況。我沒有自己嘗試,所以我不知道,這種方法很有可能會在未來的版本中被打破。
但也許有另一種方法可以爲你工作。正如我從參考問題中看到的那樣,您正在通過意向啓動一項活動。如果這個活動是用於編輯設置的,那麼這不是正確的方法,因爲android只是用這個意圖來啓動一個活動而已。在我看來,最好是通過擴展任何可用的特性來定製特定的偏好活動。這是我的個性ListPreference,我使用以便讓用戶選擇的應用程序:
public class CustomSelectAppPreference extends ListPreference {
//----- Constructor -----
public CustomSelectAppPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
//----- Constructor END -----
//----- Public Code -----
public void SetResult(String packageName) {
if(this.callChangeListener(packageName)) {
Editor edit = this.getSharedPreferences().edit();
edit.putString(this.getKey(), packageName);
edit.commit();
}
this.getDialog().dismiss();
}
//----- Public Code END -----
//----- ListPreference Overrides -----
@Override
protected void onPrepareDialogBuilder(Builder builder) {
Log.d("CustomSelectAppPreference", "onPrepareDialogBuilder");
super.onPrepareDialogBuilder(builder);
String selectedPackage = this.getSharedPreferences().getString(this.getKey(), "");
ListAdapter listAdapter = (ListAdapter) new ApplicationsArrayAdapter(this.getContext(), Utilities.getInstalledApplications(this.getContext(), selectedPackage), this);
builder.setAdapter(listAdapter, this);
}
//----- ListPreference Overrides END -----
}
,我使用它在我的preferences.xml這樣的:
<PreferenceScreen android:key="connection_screen"
android:title="@string/wpref_Connection_Screen_title"
android:summary="@string/wpref_Connection_Screen_summary"
android:shouldDisableView="true">
<com.test.app.CustomSelectAppPreference android:key="userSelectedApplication"
android:title="@string/wpref_userSelectedApplication_title"
android:summary="@string/wpref_userSelectedApplication_summary"
android:dialogTitle="@string/userselectedApplication_dialogTitle"
android:entries="@array/selectedapps_dummy_actions"
android:entryValues="@array/selectedapps_dummy_actionsvalues"
android:defaultValue=""
android:shouldDisableView="true"/>
</PreferenceScreen>
通過使用這種方法,我可以控制我的用戶在此活動中執行的所有操作,而不會破壞有關偏好的android規則
希望這會有所幫助。
可能的重複:http://stackoverflow.com/q/3385075/291827 – 2013-01-06 10:55:57