說我們有兩個活動,Activity1和Activity2。android-如何在活動結束後顯示對話框
在活動1的onClick()方法中,我們有一個呼叫開始活動2如果按下某個按鈕:
Intent myIntent = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(myIntent);
結束後()被調用的活性2,和活性1重新開始,我需要一個對話框以便在恢復後立即顯示在Activity1中。
之前,我只是叫的ShowDialog(ID)的活性1的onClick同一塊()方法:
public void onClick(View v) {
if(v == addHole){
//...
Intent myIntent = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(myIntent);
showDialog(END_DIALOG_ID);
}
}
的問題是,活性1恢復後,對應END_DIALOG_ID對話是不可見的,但屏幕變暗且無響應(就像對話存在一樣),直到按下後退鍵。
我已經嘗試在Activity1的onResume()和onRestart()方法中放置showDialog()調用,但這些都會使程序崩潰。
我也嘗試在Activity2中創建一個AsyncTask方法,並在其onPostExecute()中調用showDialog(),但該對話框在Activity2中不可見。
private class ShowDialogTask extends AsyncTask<Void, Void, Integer> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Integer doInBackground(Void... id) {
//do nothing
return END_DIALOG_ID;
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Integer id) {
super.onPostExecute(id);
showDialog(id);
}
}
我現在想通過調用
Activity1.this.startActivityForResult(myIntent, END_DIALOG_REQUEST);
與相應的setResult()和活性1 onActivityResult()方法來實現這一點,但似乎應該有實現這更好的做法。我需要的是在Activity2完成時顯示一個對話框。
感謝您提供的任何幫助。
我打算建議startActivityForResult和onActivityResult,但你似乎已經自己發現了它。對我而言,這是實現你想要的最直接的方法。 – mportuesisf