appukrb
appukrb

Reputation: 1507

Close activities which are running in background

i have design an application where it have many activities and one activity is called from broadcast receiver and running, how to close the activities which are running related to the application or how to close other activity using new activity of the application

Upvotes: 1

Views: 3363

Answers (2)

appukrb
appukrb

Reputation: 1507

At last i find the solution it is simple set flag

Intent i1 = new Intent(context, Your class name);  
i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // <= USE THIS
startActivity(i1);

Upvotes: 2

prozhyga
prozhyga

Reputation: 439

Permissions:

 <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="16" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

Code

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(100);
for (ActivityManager.RunningTaskInfo taskInformation : taskInfo) {
    if (yourApplicationPackageName.equalsIgnoreCase(taskInformation.baseActivity.getClassName())) {
        String pack = taskInformation.baseActivity.getClassName().substring(0, taskInformation.baseActivity.getClassName().lastIndexOf("."));
        am.killBackgroundProcesses(pack);
    }
}

Upvotes: 0

Related Questions