您完全可以從您在xml中定義的配置活動完成此操作。只是讓你的第一個活動開始一個意圖,你的第二個活動,但使用startActivityForResult()方法。然後在第二個活動中,當用戶在第二個活動中單擊完成按鈕時,使第二個活動調用finish()
方法。但在致電完成之前,請將結果設置爲您在第二個活動中收集的所有數據。控制將返回到第一個活動,您可以在onActivityResult()
方法中處理從第二個活動中獲得的結果。然後,將第二個活動的結果添加到您要從此活動返回的結果中。
好吧,讓我們來看看這個準系統的一個例子。
ConfigActivity1 extends Activity{
protected onCreate(Bundle icicle){
//do your setup stuff here.
//This is the button that's going to take us to the next Config activity.
final Button nextConfig = (Button)findViewById(R.id.next_config);
//We'll add an onClickListener to take us to the second config activity
nextConfig.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
//Construct Intent to launch second activity
Intent furtherConfigIntent = new Intent(ConfigActivity1.this, ConfigActivity2.class);
//By using startActivityForResult, when the second activity finishes it will return to
//this activity with the results of that activity.
startActivityForResult(furtherConfigIntent, 0);
}
});
//finish any other setup in onCreate
}
//This is a callback that will get called when returning from any activity we started with the
//startActivityForResult method
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(resultCode == Activity.RESULT_CANCELED){
//this means the second activity wasn't successfull we should probably just
//return from this method and let the user keep configuring.
return;
}
//ok, if we made it here, then everything went well in the second activity.
//Now extract the data from the data Intent, compile it with the results from this
//acitivity, and return them. Let's say you put them in an Intent called resultsIntent.
setResult(Activity.RESULTS_OK, resultsIntent);
finish();
}
}
第二個acitvity會非常簡單。只需收集你的配置數據,當用戶按下完成時,將結果數據和resultCode設置爲OK,然後完成。
謝謝。它看起來像這回答我的問題。 :) – Jakar
問題是關於一個小部件。但答案是關於一個應用程序(這更容易)。所以我不明白這是如何回答這個問題的。 –
@BobUeland問題是相當開放的,但它確實說「我的小工具應用程序」,我覺得給了我一個相當廣泛的問題來回答它的許可證。提問者似乎也認爲我回答了這個問題。 如果您希望獲得更具體的內容,可能會提出一個新問題。 –