Naseeb Sheoran
Naseeb Sheoran

Reputation: 473

How to remove an activity from a the stack

I am making an application in which i want to make sure that only one instance of a single activity should remain in the stack.Please tell me how to achieve that.This is the coding which i was trying but have not got the intended result.

ArrayList<String> runningactivities = new ArrayList<String>();

    ActivityManager activityManager = (ActivityManager)getBaseContext().getSystemService (Context.ACTIVITY_SERVICE); 

    List<RunningTaskInfo> services = activityManager.getRunningTasks(Integer.MAX_VALUE); 

        for (int i1 = 0; i1 < services.size(); i1++) { 
            runningactivities.add(0,services.get(i1).topActivity.toString());  
        } 

        if(runningactivities.contains("ComponentInfo{com.velosys.interview_preparation/com.velosys.interview_preparation.activities.MCQ}")==true){
             Toast.makeText(getBaseContext(),"Activity is in foreground, active",1000).show(); 
            //MCQ.
        }

Thanks in advance

Upvotes: 0

Views: 412

Answers (3)

sachy
sachy

Reputation: 789

you can set intent flags while launching your activities. In your case FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP

Upvotes: 0

jenzz
jenzz

Reputation: 7269

That's a typical case for an Activity Launch Mode.

The official Android documentation is pretty precise and coherent imho.

Check out the paragraph android:launchMode. The singleTask mode might be what you are looking for.

Upvotes: 1

Panos
Panos

Reputation: 7407

put inside the onStop() or the onPause() methods of the activity the "finish()" command. So every time you exit this activity it will be removed from the activity stack , so there will be no duplicate.

Upvotes: 0

Related Questions