1

I want my activity to be clear form the back stack when user press back button. I am using this intent to open my activity:

Intent i = new Intent(context, OutGoingActivity.class);
i.putExtra("Number", incomingNumber);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);

I want my OutGoingActivity Activity will be removed from the stack when I press the back button.

Every time when ever I launch my activity, a new activity should be opened instead of from the stack.

Bowdzone
  • 3,827
  • 11
  • 39
  • 52
Tarun Bhutani
  • 49
  • 1
  • 4

2 Answers2

5

Just use finish(); for remove current activity.

Like

Intent i = new Intent(context, OutGoingActivity.class);
i.putExtra("Number", incomingNumber);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(i);
// use finish()
finish();

When you call finish() method, onDestroy() method will be execute.

Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
-1

If you want to remove your activity from stack then you can use below code :

    android.os.Process.killProcess(android.os.Process.myPid());
    System.exit(1);

     Intent mintent = new Intent(context, OutGoingActivity.class);
     mintent .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
     context.startActivity(mintent);
     finish();

But if you want to remove all the activities then you should try below code :

ActivityCompat.finishAffinity(OutGoingActivity.this);
finish(); 

Hope it will help you.

KishuDroid
  • 5,411
  • 4
  • 30
  • 47
  • Thanks for your replay. No it is not working, i am launching my OutGoingActivity from a broadcast receiver class the triggers on every incoming call. Now the problem is, first time it launch my activity over answering screen and when i click on end button my activity move to the stack and when i got the incoming call second time, my activity launch from the stack and this time answering screen come on the top of my activity. I want to my activity will always be visible on top of the answering screen. – Tarun Bhutani Dec 01 '15 at 07:19
  • @TarunBhutani : please see my edit. – KishuDroid Dec 01 '15 at 07:26
  • @TarunBhutani then do one thing, if you want to remove activity on end button click, then call `finish()` on end button. – Chirag Savsani Dec 01 '15 at 08:23
  • I have used finish() as well as the above, but none of them have solved my problem. I can still see my task in the task bar. – Tarun Bhutani Dec 01 '15 at 10:06