How To Stop Multiple Instances For An Activity
Posted on August 25th, 2010 in Android | 7 Comments »
To make visible an activity that is already running or to create an activity that is not currently running, add android:launchMode=”singleTask” in activity element in AndroidManifest.xml.
For example an activity “SingleTaskActivity” for that we want to stop multiple instances, its entry in “AndroidManifest.xml” is as;
<manifest> --------------------------------------------------------------------- <activity android:name=".SingleTaskActivity" android:launchMode="singleTask" android:label="@string/app_name"> </activity> -------------------------------------------------------------------- </manifest>
Here is a complete working example;
src>com.devaxis.apis:
I have created two classes HomeActivity.java and SingleTaskActivity.java.
HomeActivity.java
public class HomeActivity extends Activity {
private static final long DEFAULT_DELAY = 5000;
private static final long DEFAULT_PERIOD = 5000;
private Handler handler;
private Timer timer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
timer = new Timer();
handler = new Handler();
// Schedule a task to start activity for repeated fixed-rate execution after 5 seconds.
startSingleTaskActivity(DEFAULT_DELAY,DEFAULT_PERIOD);
}
public void onBackPressed ()
{
timer.cancel();
finish();
}
public void startSingleTaskActivity(long delay,long period)
{
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
startActivity(new Intent(HomeActivity.this, SingleTaskActivity.class));
}
});
}
};
timer.scheduleAtFixedRate(timerTask, delay,period);
}
}
In HomeActivity.java A Timer with TimerTask is used to schedule a task to start activity for repeated fixed-rate execution after 5 seconds.
SingleTaskActivity.java
public class SingleTaskActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.single_task_activity);
}
}
res>layouts:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle = "bold" android:text="@string/home_activity_label" /> </LinearLayout> single_task_activity.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle = "bold" android:text="@string/single_task_activity_label" /> </LinearLayout>
res>values:
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="home_activity_label">Home Activity!</string> <string name="app_name">Stop Multiple Activity Instances</string> <string name="single_task_activity_label">Single Task Activity!</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" package="com.devaxis.apis" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".HomeActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SingleTaskActivity" android:launchMode="singleTask" android:label="@string/app_name"> </activity> </application> </manifest>
Download complete source code
StopMultipleActivityInstancesDemo.zip (46.7 KiB, 79 hits)
7 Responses
hey, nice blog…really like it and added to bookmarks. keep up with good work
thanks for the ideas , i’d love to stick to your weblog as generally as i can.have a great day
When I search for blogs I never know what I will find and that is half of the fun of it really. I was surprised I ran across yours though. It is excellent writing though. You have talent in there – keep it up.
Excellent information here. This interesting post made me smile. Maybe if you throw in a couple of pics it will make the whole thing more interesting.
hello…
really good article. Ready to hear more next week,my blog http://thehotspotguide.com/profile_blog_full.php?id=28983 Many Thanks….
Great One…
I must say, its worth it! My link, http://blog.nrj.fr/carina11,thanks haha…
Hi…
http://www.webcamgirls4.com/...