I having some trouble with bringing a singletask activity back into view.
Manifest:
<activity
android:name=".test.MyActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:icon="@drawable/my_launcher"
android:label="@string/title_activity_my"
android:launchMode="singleTask"
android:taskAffinity=".myTask"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
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!
startActivity(i);
Scenario:
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);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
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.
UPDATE (1):
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.
Results:
(i) If I start the timer via btnStartTimer click, and do not press btnMoveToBack, then the intent code is called and 'B' appears.
(ii) If I start the timer via btnStartTimer click, and DO press btnMoveToBack, then the intent code is still called (indicated by log msg) 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?
I finally cracked this one. As it appears, an intent (with any consequence) cannot be fired from a paused (
singleTask
) Activity. Therefore my only option was to implement anIntentService
(utilisingIntent
flagsFLAG_ACTIVITY_CLEAR_TOP
andFLAG_ACTIVITY_NEW_TASK
) which in turn creates the required intent to 're-show' mysingleTask
Activity.Seems like a bit of a sledgehammer to crack a walnut, but it is the only method I could find that worked.