机器人 - 如何显示对话活动结束后 [英] android - How to show dialog after activity finishes
问题描述
假设我们有两个活动,活动1和活性2。
Say we have two activities, Activity1 and Activity2.
在活动1的onClick()方法中,我们有一个呼叫如果某个按钮pressed开始活动2:
In Activity1's onClick() method, we have a call to start Activity 2 if a certain button is pressed:
Intent myIntent = new Intent(Activity1.this, Activity2.class);
Activity1.this.startActivity(myIntent);
完成()被称为活性2后,与活动1恢复,我需要一个对话框,在活动1显示,只要它被恢复。
After finish() is called in Activity2, and Activity1 is resumed, I need a dialog to show in Activity1, as soon as it is resumed.
之前,我简单地称为的ShowDialog(ID)的活性1的onClick的同一块()方法:
Before, I simply called showDialog(id) in the same block of Activity1's onClick() method:
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对话框不可见,但屏幕变黑,反应迟钝(好像对话是present),直到返回键是pressed。
The issue is, after Activity1 resumes, the dialog corresponding to END_DIALOG_ID is not visible, but the screen is darkened and unresponsive (as if the dialog were present), until the back key is pressed.
我试图把在活动1的onResume()和onRestart()方法调用ShowDialog()调用,但这些都引起程序崩溃。
I have tried putting the showDialog() call in Activity1's onResume() and onRestart() methods, but these both crash the program.
我也试图创造一种活性2的方法AsyncTask的,在它的onPostExecute()ShowDialog的()调用,但该对话框是不可见的活性2。
I have also tried creating an AsyncTask method in Activity2, with the showDialog() call in its onPostExecute(), but the dialog is not visible in 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);
}
}
我现在试图通过调用来实现这个
I am now trying to implement this by calling
Activity1.this.startActivityForResult(myIntent, END_DIALOG_REQUEST);
与相应的setResult()和的onActivityResult()的活性1的方法,但似乎应该有实施这种一个更好的做法。所有我需要的是在活性2整理已显示出一个对话框。
with corresponding setResult() and onActivityResult() methods from Activity1, but it seems that there should be a better practice for implementing this. All I need is to have a dialog shown upon Activity2 finishing.
感谢您的帮助,您可以提供。
Thanks for any help you can provide.
推荐答案
就像你的建议,呼吁 startActivityForResult
启动时活性2
。然后,覆盖的onActivityResult
和检查 RESULT_OK
,并显示您的对话框即可。这就是做你希望做一个完全可以接受的做法。
Like you suggest, call startActivityForResult
when starting Activity2
. Then, override onActivityResult
and check for RESULT_OK
, and show your dialog box then. That's a perfectly acceptable practice for doing what you're looking to do.
这篇关于机器人 - 如何显示对话活动结束后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!