1

What I want to do is to launch from the MainActivity in a new Thread a procedure that:

1) Makes an Internet call

2) Extract something like a boolean from the response (that represent which AlertDialog to show)

3) Show the correct (depending on the value of the boolean) AlertDialog in the MainActivity

I have no problems in executing first two steps using a Runnable or a AsyncTask but I can't show the alert in MainActivity

I followed different guides but I couldn't reach the solution.

Solved

My MainActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mHandler=new Handler();
    new Thread(new UpdateRunnable(this,mHandler)).start();
    /* Other stuff */
}

My Runnable:

public class UpdateRunnable implements Runnable{

private Activity activity;
private Handler mHandler;
private boolean update;
/* Other stuff */


public UpdateRunnable(Activity activity, Handler mHandler){
    this.activity = activity;
    this.mHandler = mHandler;
    /* Other stuff */
}

@Override
public void run() {
    // It get from web which Dialog to call
    update = update();
    // It manage the UI work
    mHandler.post(new Runnable() {
        public void run() {
            if(update){
                show_dialog_you_are_not_updated();
            } else {
                show_dialog_you_are_updated();
            }
        }
    });
}
/* The methods update, show_dialog_you_are_not_updated, show_dialog_you_are_updated */
}
madx
  • 6,723
  • 4
  • 55
  • 59
  • I controlled if the application is updated from the internet, then 1) if it is updated I want to show an AlertDialog "You are updated!" 2) if it is not updated I want to show "You are not updated" --> on click on YES I send the user to the Google Play Store with: startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName))); – madx Jan 12 '14 at 17:20
  • please show the code where you are getting problems – Rohan Kandwal Jan 12 '14 at 17:54
  • Ok :D I edited my question and added my code. – madx Jan 13 '14 at 12:43
  • why aren't you trying to show AlertDialog on your MainActivity? – Rohan Kandwal Jan 14 '14 at 06:35
  • Scuse me, could you show me how? To know which Dialog to show I need to asynchronously get the boolean from the Web. So I can't understand how to notify MainActivity to show the dialog after the web request; I also tried with the Observer Patter but it does't work (MainActivity = Observer, UpdateRunnable = Observed), It seems that I can't show the Dialogs in this way... – madx Jan 14 '14 at 12:09
  • You can always show the AlertDialog in your if and else statement.See http://stackoverflow.com/a/2115770/1979347 on how to create AlertDialog. In place of `this` just pass `activity`. – Rohan Kandwal Jan 14 '14 at 16:35
  • The problem it's that it's forbidded to call a dialog normally from a Thread, so i solved my problem using an Handler inside the Runnable, I pasted in my edited answer. – madx Jan 14 '14 at 23:39
  • If your Runnable is inside MainActivity then you can make a alert function in MainActivity, pass some variables and use runOnUIThread() to generate AlertDialog on main UI – Rohan Kandwal Jan 15 '14 at 05:57
  • Ok I'll try to make the best of your advice thanks again Rohan. :D – madx Jan 15 '14 at 11:07
  • If my advice works for you then I will post that as an answer so that you can accept it and mark this question solved. If not, post a comment again and I'll try to help – Rohan Kandwal Jan 15 '14 at 17:49

0 Answers0