Thursday, October 28, 2010

How to restart your own app in Android Tutorial

After launching (now refactoring) Shoot and Learn [ Market Link | AndroidPit.com Link ] , its time to write what i have learn.

Senario
You want to restart your app because of some preference/setting changed, or you just want to restart your app.

Notes
This might not be the best solution thus if you have a better one kindly comment below :)

Introduction
First we need our main activity to be a routing activity (if you came from the web, most framework do this where you have an index page where it routes other calls to its right controller). This main activity we shall call PreMainActivity, in this tutorial we would only put the restart logic on this activity, you can put things like preloader, first run activity, etc.


What Do I Need
In Manafiest.xml
<activity 
 android:name=".PreMainActivity"
 android:label="@string/app_name"
 android:launchMode="singleTop"
 android:configChanges="locale"
>
 <intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />
 </intent-filter>
</activity>
<activity android:name=".Main"
 android:configChanges="locale"
/>



In PreMainActivity.java
public class PreMainActivity extends Activity {
 public static Boolean ENABLE_RESTART = false;
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  ENABLE_RESTART = true;
  restartMain();
 }
 
 @Override
 public void onRestart() {
  super.onRestart();
  restartMain();
 }
   
 public void restartMain(){
  if(ENABLE_RESTART == true){
   Intent mainIntent = new Intent(this, Main.class);
   mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP  );
   startActivity(mainIntent);
   finish();
  }else{
   finish();
  }
  ENABLE_RESTART   = false;
 }    
}


In your activity where you want to trigger the restart
PreMainActivity.ENABLE_RESTART = true;
Intent i = new Intent(this,PreMainActivity.class);
i.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);



Explanation
<activity

android:name=".PreMainActivity"

.... />

As stated in our introduction we would use a PreMainActivity as the receiver of our main action.

public static Boolean ENABLE_RESTART = false;
In our activity we would have a static variable to check if the activity should restart or not.

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ENABLE_RESTART = true;
restartMain();
}

When our app is first launched we would need to turn the flag to true in order for our next activity to restart/start.

Intent mainIntent = new Intent(this, Main.class);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity(mainIntent);
finish();

With this code, i assumed that main is on the top of the activity stack therefore we could just relaunch it as clear the whole stack. See (http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP).

PreMainActivity.ENABLE_RESTART = true;
Intent i = new Intent(this,PreMainActivity.class);
i.addFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);

Combined with android:launchMode="singleTop" in our manifest file, we make sure that when we launch PreMainActivity we get a clear top, single top activity. Note I might be doing some redundancy on singletop flag.

Conclusion
This how you can restart your app but this is not the best solution, as stated above im in a refactoring mode, in where instead of having a static variable, i would pass data i.putExtra("RESTART", true); on the intent. Which is a better solution, but if you have any better idea please do comment below and ill try to be more active with this blog :)
Post a Comment