multithreading - Kill a thread in previous activity in android -


i have thread in activity starts activity b after 30 seconds.but user can go activity b before time on button click.i want kill thread in activity if user clicks button activity b wont started again. tried kill thread if button clicked, of no use , finish() not killing thread after navigating b.

thread t=new thread()     {         public void run()         {                 try {                 sleep(5000);                 currentclass = class.forname("com.crazydna.memorizethepic.level"+levelnumber);                 intent ourintent = new intent(picturedisplay.this, currentclass);                 startactivity(ourintent);             }              catch (classnotfoundexception e) {                 // todo auto-generated catch block                 log.e("tag","error: " +e.getstacktrace());                 //e.printstacktrace();                 alertdialog.builder alertdialog=new alertdialog.builder(picturedisplay.this);                 alertdialog.settitle("alert!!!");                 alertdialog.setmessage(" "+e.tostring());                 alertdialog.setneutralbutton(android.r.string.ok, new dialoginterface.onclicklistener() {                        public void onclick(dialoginterface dialog, int id) {                            dialog.dismiss();                                                }                    });                 alertdialog.show();             } catch (interruptedexception e) {                 // todo auto-generated catch block                 e.printstacktrace();                 alertdialog.builder alertdialog=new alertdialog.builder(picturedisplay.this);                 alertdialog.settitle("alert!!!");                 alertdialog.setmessage(" "+e.tostring());                 alertdialog.setneutralbutton(android.r.string.ok, new dialoginterface.onclicklistener() {                        public void onclick(dialoginterface dialog, int id) {                            dialog.dismiss();                                                }                    });                 alertdialog.show();             }         }     };     t.start(); 

you have options.

the first option shorten time sleep() function , enclose in while() block monitor cancellation variable, variable in class definition.

boolean run_my_timer = true; while (run_my_timer) {     sleep(1000); // sleep 1 second     currentclass = class.forname("com.crazydna.memorizethepic.level"+levelnumber);     intent ourintent = new intent(picturedisplay.this, currentclass);     startactivity(ourintent); } 

and add line set's variable false if user clicks button

run_my_timer = false; 

this make thread exit.

the second option, more elegant create timer, instead of thread, if user presses button open activityb cancel timer, timer's cancel() method.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -