Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use IntentCompat.makeRestartActivityTask()?

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?

like image 316
RenniePet Avatar asked Oct 04 '13 13:10

RenniePet


1 Answers

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.

like image 117
Maragues Avatar answered Sep 28 '22 11:09

Maragues