Restarting a singleTask activity

I am having some trouble with bringing a singletask activity back into view.


    android:theme="@android:style/Theme.Black.NoTitleBar" >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />

Open in new window

MyActivity Code:

Intent i = new Intent(getApplicationContext(), MyActivity.class);
i.putExtra("blah", stuff);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK); // I have tried almost every flag combo here!

Open in new window


Upon onBackPressed() the activity calls moveTaskToBack (which hides my activity). Upon receipt of a specific incoming event, the above intent code is called, however NOTHING happens!?! No OnNewIntent() fired, no onCreate fired...

HOWEVER: If I also add a separate PendingIntent to a Notification object, it WORKS!?

    Intent notificationIntent = new Intent(getApplicationContext(),MyActivity.class);
    notificationIntent.putExtra("blah", stuff);
    PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    Notification notif = new Notification(icon,text,when);
    notif.flags |= Notification.FLAG_ONGOING_EVENT;
    notif.setLatestEventInfo(getApplicationContext(), contentTitle, contentText, contentIntent);
    m_notifMgr.notify(1, notif);

I don't understand why it works from a PendingIntent (firing onNewIntent), but not from a direct startActivity call.

Please advise. Thanks.
Who is Participating?
brenlexConnect With a Mentor Author Commented:
I finally found a method that works.  As it appears, an intent (with any consequence) cannot be fired from a paused (singleTask) Activity. Therefore my only option was to implement an IntentService (utilising Intent flags FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK) which in turn creates the required intent to 're-show' my singleTask Activity.
brenlexAuthor Commented:
Let's simplify my question -- after using moveTaskToBack to 'hide' a singleTask activity... how can I make it re-appear?
Try adding these lines to your activity launched


Open in new window

The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

brenlexAuthor Commented:
No difference.

I have simplified matters and placed the same logic into a small test app:
Activity 'A' = singleTask, with two buttons: btnStartTimer to start a timer, and btnMoveToBack to move 'A' to back.  When the timer expires, the intent to create 'B' is called.
Activity 'B' = plain 'hello world' screen.

(i) If I start the timer via btnStartTimer click, and do not press btnMoveToBack, then the intent is created and 'B' appears.
(ii) If I start the timer via btnStartTimer click, and DO press btnMoveToBack, then the intent is still created and 'B' does NOT appear -- logging shows Activity 'B' onCreate NOT called.

Which raises the question -- can I actually get intents to be processed from an activity that is not shown?
Hi  brenlex,

Just wanted to make sure I understand, so I have a few questions:

1)  Hidden Activity A (hidden by the Android native method Activity.moveTaskToBack(boolean) ) is supposed to receive an event message. After receiving the event message, Activity A will start Activity B?

2)  What kind of message is Activity A waiting for, a system message, a message from your App, or a message from another App, what's the nature of the message?
brenlex when another activity comes into the foreground (lets say activity B) the previous activity is paused (the onPause method is called). Lets call this previous activity A. The activity B that comes into the foreground will not be created until the onPause method of activity A returns.

Where do you have the code that starts the event? As an experiment try to put it in the pause method of activity A and see what happens.
brenlexAuthor Commented:
I have taken a step back and thought about this... when using moveTaskToBack, Activity 'A' actually becomes a paused activity -- is it actually possible to launch intents from a paused activity?

Background to actual app requirement (as opposed to the test app):
- With regard to the actual app, there is no Activity 'B' equivalent.  I was just trying to make Activity 'A' show itself again when the event occurs.  It was only when I was failing to get the Intent to work that I started testing with a secondary ('B') Activity.
- The actual app uses a third party API which triggers a callback event, which resides on the singleTask Activity 'A'.  On occurrence of this event I just wanted to 'show' Activity 'A' again, but have been failing (hence this post).

Presently my actual app behaviour (as indicated on initial post) is that I create a PendingIntent to a Notification object, which notifies the user of the event, allowing them to bring Activity 'A' back into view by pressing on the notification drawer entry.  This is fine, but I also want Activity 'A' to come back into view when the event occurs, without the necessity for notification bar user interaction.
but I also want Activity 'A' to come back into view when the event occurs, without the necessity for notification bar user interaction.

Then have activity B listening for this event and when it occurs it can bring activity A to front.
brenlexAuthor Commented:
There is no Activity B. Besides, the point is, there is no Activity on the screen when the event occurs -- which is my problem... the event fires OK on a (singleTask) paused activity and fires the startActivity code... but nothing further happens.
PCableGuyConnect With a Mentor Commented:
Looks like you are on the right track by re-thinking it and using a Service. If you haven't read about the Activity life cycle already, here's a good explanation:

What stands out to me is that a Paused Activity can be killed by the Android OS, so it seems counter intuitive to Pause an Activity and then hope to resume it within its own code.
brenlexAuthor Commented:
It all works perfectly fine through use of IntentService with a singleTask.  

I guess the next time I will be tossing any event generating APIs onto a dedicated service with a host of Intents, with Activities solely as the GUI view into the app.

Live and learn.
brenlexAuthor Commented:
Found the resolution myself. Some points to PCableGuy for taking the time.
All Courses

From novice to tech pro — start learning today.