(re) showing a singleTask activity

2019-06-07 19:10发布

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?

2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-06-07 19:36
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if (intent.hasExtra("KEY")) {
        int currentTab = intent.getIntExtra("KEY", 1);
        tabHost.setCurrentTab(currentTab);
    }
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-06-07 19:49

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 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.

Seems like a bit of a sledgehammer to crack a walnut, but it is the only method I could find that worked.

查看更多
登录 后发表回答