java - Making a Button restart the activity -


ok know may seem simple, mean thought was. want restartactivity method restart activity.

first thing did create button , had line of code in xml such. android:onclick="restartactivity"

then class contains restartactivity method such

public void restartactivity() {     intent intent= new intent(this, mainactivity.class);     startactivity(intent); } 

when try ends happening stopped working dialog box. question why not working. made sure made restart button in right layout, checked make sure needed references made. , android manifest correct. wasn't until added 2 lines started crashing. should mention target api 8 , 1 of answers suggested api 11 or higher. eclipse "kindly" let me knew.

activity class provides method:

 public void recreate () 

since: api level 11 cause activity recreated new instance. results in same flow when activity created due configuration change -- current instance go through lifecycle ondestroy() , new instance created after it.

so can this,

public void restart() {    this.recreate(); } 

if outside of activity, simply:

public void restart(context ctx)     {        activity = (activity)ctx;        ctx.recreate();     } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -