Say we have two activities, Activity1 and Activity2.
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);
After finish() is called in Activity2, and Activity1 is resumed, I need a dialog to show in Activity1, as soon as it is resumed.
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);
}
}
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.
I have tried putting the showDialog() call in Activity1's onResume() and onRestart() methods, but these both crash the program.
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);
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With