android - How to create a dialog box in a non-UI thread in a different class? -


i'm developing simple game in android(that runs in non-ui thread), want make that, when game over, shows custom dialog box score, class isn't in mainactivity class. can't figure out how create dialog in thread whitout getting error.

there many ways that. 1 way pass context class constructor of game able access ui through it.

public class mygame {    private context mcontext;    private handler handler;     public myclass(context context) {       this.mcontext = context;       handler = new handler(looper.getmainlooper());    }    ... } 

and when initializing activity

mygame game = new mygame(this); 

and show dialog in game class, use code

handler.post(new runnable() {    public void run() {       // instanitiate dialog here       showmydialog();    } }); 

and how show simple alertdialog.

private void showmydialog() {   new alertdialog.builder(mcontext)     .settitle("som title")     .setmessage("are sure?")     .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {         public void onclick(dialoginterface dialog, int which) {              // continue delete         }      })     .setnegativebutton(android.r.string.no, new dialoginterface.onclicklistener() {         public void onclick(dialoginterface dialog, int which) {              // nothing         }      })     .seticon(android.r.drawable.ic_dialog_alert)     .show(); } 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -