12

I'm trying to implement a button that will result in my app going back to the first activity and acting as if it was (almost) restarted all over. This code

Intent newIntent =
        new Intent(currentActivity.getApplicationContext(), StartActivity.class);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK + Intent.FLAG_ACTIVITY_CLEAR_TASK);
currentActivity.startActivity(newIntent);

seems to be working OK for a newer tablet that is running Android 4.1, but it doesn't work on an older device that is running Android 2.3.4.

I've found a couple of threads about this:

Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK not working Android

Clear all activities in a task?

Reading the fine print leads me to believe I should be using the IntentCompat class in android-support-v4.jar, http://developer.android.com/reference/android/support/v4/content/IntentCompat.html

Unfortunately, the documentation does not contain any examples, and I'm very unsure of how I should be using IntentCompat. The only example I've found is this: Not start MainActivity with android 2.3

which leads me to believe I should be doing something like this:

    Intent newIntent = IntentCompat.makeRestartActivityTask(cn);

But this is giving me a compiler error, saying "makeRestartActivityTask" is an undefined symbol.

I'm guessing this implies I haven't added android-support-v4.jar correctly to my build environment (IntelliJ IDEA 12 community edition), but I've tried doing that in several different ways, and it still doesn't work.

So I have two questions:

  1. Does my attempted usage of IntentCompat look correct?

  2. How do I get the compiler to stop saying that "makeRestartActivityTask" is an undefined symbol?

Community
  • 1
  • 1
RenniePet
  • 11,420
  • 7
  • 80
  • 106

3 Answers3

26

Update

Google has removed the method IntentCompat.makeRestartActivityTask() in current support library versions. Instead, you can just use the plain Android API:

ComponentName cn = intent.getComponent();
Intent.makeRestartActivityTask(cn);

I hope this can save someone time searching for alternatives ;)

ByteHamster
  • 4,884
  • 9
  • 38
  • 53
22

This is how I'm using IntentCompat

    Intent intentToBeNewRoot = new Intent(this, MainActivity.class);
    ComponentName cn = intentToBeNewRoot.getComponent();

    Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);

    startActivity(mainIntent);

This effectively replaces my no-longer-wanted task root with MainActivity. It works in Gingerbeard and ICS. I haven't seen the "is an undefined symbol" message.

Maragues
  • 37,861
  • 14
  • 95
  • 96
  • Thanks for your answer. Could you please tell me which jar file you've added to your project which contains the IntentCompat class? Are you using IntelliJ or Eclipse? – RenniePet Oct 11 '13 at 10:47
  • I'm using android-support-v4.jar, version 18. I'm on eclipse, but I use gradle for my final builds and it works too. – Maragues Oct 11 '13 at 10:57
  • OK, thank you very much. My mistake was that the version of android-support-v4.jar that I'd added to my project was apparently prior to version 18. – RenniePet Oct 11 '13 at 13:12
  • 4
    this solution work for Android 4, but is not working for Android 2.3. It just does nothing! any idea? – Alvaro Luis Bustamante Feb 03 '14 at 15:57
  • which support-library version are you using? I recall testing this for v4:18.0 or v4:19.0, tho I haven't tried in a long time – Maragues Feb 04 '14 at 09:31
  • Not working for me in Gingerbread. I'm using support library v19.1, and a previous version didn't work as well. – Mister Smith May 13 '14 at 08:24
  • Not working with PendingIntent in GingerBread. any idea please – Zeeshan Jan 01 '15 at 10:19
  • 1
    Read the update below from @ByteHamster ... it was removed in the v27 support library – Matei Suica Feb 03 '18 at 08:34
1

IntentCompat is deprecated which is may be deleted, but Intent class this static method

Intent mainIntent = Intent.makeRestartActivityTask(cn);

So just use the above statement.

Karthikkumar
  • 87
  • 1
  • 7