8

well, so, that´s my problem.

I need, when the user press the back bottom, to delete the stack of all activities opened:

@Override
public void onBackPressed(){
    Intent i = new Intent(context, CrisolMainApp.class);
    i.putExtra(CrisolMainApp.CERRAR_APP, true);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
}

The point is that this is working with most of my phones, but yesterday I tried with a Samsung Galaxy Mini (android 2.3.3) and it is not working!! It is not deleting the stack of activities.

Anyone knows why?

EDIT: I was checking on doc, and I found out that the "Intent.FLAG_ACTIVITY_CLEAR_TASK" was added on API 11(my Samsung Galaxy Mini is working for Android 2.3.3, so, API 10).......I don´t know, but I guess that´s the problem.

Anyone knows how can I make it work the same way for API less than 11?

I was trying by adding these flags and it is not working either:

@Override
public void onBackPressed(){
    Intent a = new Intent(this, CrisolMainApp.class);
    a.putExtra(CrisolMainApp.CERRAR_APP, true);
    a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    a.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(a);
    finish();
}

and I also tried:

@Override
    public void onBackPressed(){
        Intent a = new Intent(this, CrisolMainApp.class);
        a.putExtra(CrisolMainApp.CERRAR_APP, true);
        a.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        a.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(a);
        finish();
    }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
zapotec
  • 2,628
  • 4
  • 31
  • 53
  • So did you find out why isn't it working? I am facing the same problem and feel stuck forever – Gutyn Apr 11 '17 at 22:00
  • It was a new FLAG for Android 11. So you have to check the version programatically and act in consequence! It is not working only on – zapotec Apr 12 '17 at 09:14

5 Answers5

3

A simple fix should be adding the xml attribute android:noHistory="true" to all of your activities, which will mean that as soon as the user leaves that Activity, it will be gone, and no back stack will be stored

Matt Taylor
  • 3,360
  • 1
  • 20
  • 34
  • 2
    Yes, that could be a solution, but if I do that, every times the Activity are gonna dissapear, and that´s not what I want. I just want them to dissapear when I click on Back Button...... Thanks anyway! – zapotec Jan 16 '13 at 12:21
1

You do not finish the activity.

Intent i = new Intent(context, CrisolMainApp.class);
i.putExtra(CrisolMainApp.CERRAR_APP, true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
finish();
Leandros
  • 16,805
  • 9
  • 69
  • 108
  • Thanks, but it is not working either...... I added the finish() method after the startActivity, but still not working. The weird part is that it is working on every phones I was trying before....(Samsung Galaxy SII, Galaxy Nexus, LG Optimus Black).......it is just not working for the Samsung Galaxy Mini. – zapotec Jan 15 '13 at 16:03
  • 5
    use IntentCompat instead of Intent. FLAG_ACTIVITY_CLEAR_TASK is only available in API levels >11 – Daksh Feb 25 '13 at 17:09
  • @Daksh Exactly how are you supposed to use IntentCompat? It's usage is not obvious to me. – RenniePet Oct 04 '13 at 02:22
0

You can use IntentCompat.FLAG_ACTIVITY_CLEAR_TASK and import import android.support.v4.content.IntentCompat; from support library for work on API 10.

JKLIR
  • 1,008
  • 1
  • 9
  • 11
0

if you add addFlags in Intent 'i' your intent ACTION_SEND will not work, but if add addFlags in response createChooser(), only create one instance of ACTION_SEND.

 if (i == null) i = new Intent();
            i.setAction(Intent.ACTION_SEND);
            i.putExtra(Intent.EXTRA_STREAM, getImageUri());
            i.setType(Constantes.IMAGE_STREAM);
            startActivity(Intent.createChooser(i, messageShare).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
  • you should add some text to explain how your code works, the answer isn't very clear like this. – Liam May 12 '16 at 18:07
-1

Try to add these flags:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); //use this if you want to clear the stack
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); // use this if you don't want to clear your home activity

This should clear all the activities in the stack except this one which is brought to front. You shouldn't need to use finish();

AMerle
  • 4,354
  • 1
  • 28
  • 43