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 */
}