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


&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;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"
&gt;
&lt;TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle = "bold"
android:text="@string/home_activity_label"
/&gt;
&lt;/LinearLayout&gt;

single_task_activity.xml

&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;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"
&gt;
&lt;TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle = "bold"
android:text="@string/single_task_activity_label"
/&gt;
&lt;/LinearLayout&gt;

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


&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;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"&gt;
&lt;application android:icon="@drawable/icon" android:label="@string/app_name"&gt;
&lt;activity android:name=".HomeActivity"
android:label="@string/app_name"&gt;
&lt;intent-filter&gt;
&lt;action android:name="android.intent.action.MAIN" /&gt;
&lt;category android:name="android.intent.category.LAUNCHER" /&gt;
&lt;/intent-filter&gt;
&lt;/activity&gt;
&lt;activity android:name=".SingleTaskActivity"
android:launchMode="singleTask"
android:label="@string/app_name"&gt;
&lt;/activity&gt;
&lt;/application&gt;
&lt;/manifest&gt;

Download complete source code

  StopMultipleActivityInstancesDemo.zip (46.7 KiB, 79 hits)