Android - How to show dialogue after activity completion

Let's say we have two types of activity: Activity1 and Activity2.

In the Activity1 onClick () method, we have a call to launch 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 resumes, I need to open the dialog box in Activity1 as soon as it resumes.

Before that, I simply called showDialog (id) in the same block of the Activity1 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 problem is that after the resumption of Activity1, the dialog corresponding to END_DIALOG_ID is not displayed, but the screen is darkened and does not respond (as if the dialog was present) until the back key is pressed.

I tried putting the showDialog () call into the Activity1 onResume () and onRestart () methods, but both of them crashed into the program.

I also tried to create an AsyncTask method in Activity2 with a call to showDialog () in onPostExecute (), but the dialog does not appear 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); } } 

Now I'm trying to implement this by calling

 Activity1.this.startActivityForResult(myIntent, END_DIALOG_REQUEST); 

with the appropriate methods setResult () and onActivityResult () from Activity1, but it seems that there needs to be a better practice to implement this. All I need to do is show the dialog shown after Activity2 completes.

Thanks for any help you can provide.

+6
source share
3 answers

As you suggest, call startActivityForResult when starting Activity2 . Then override onActivityResult and check RESULT_OK , and then display your dialog box. This is a perfectly acceptable practice for what you are doing.

+7
source

You can use the onResume method (if u'r is not looking for anything from Activity2)

 @Override public void onResume(){ super.onResume(); //do something } 
0
source

I need to return to the root activity - MainActivity, potentially close several actions, and then show the dialog. Therefore, I chose an alternative path.

 MyDialog { public static synchronized void planToShowDialog(String info) { if (info != null) { saveInfoToPreferences(info); } } public static synchronized void showDialogIfNecessary(Context context) { String info = readInfoFromPreferences(); if (info != null) { saveInfoToPreferences(null); // Show dialog once for given info. new MyDialog(context, info).show(); } } private static String readInfoFromPreferences() { //... } private static void saveInfoToPreferences(String info) { //... } } 

I call the MyDialog.showDialogIfNecessary () method from the MainActivity.onPostResume () method.

0
source

Source: https://habr.com/ru/post/918682/


All Articles