Before all, I need to notify that I have search on internet without success...
My problem is quite simple. My application can display at any moment a push notification (that should open Event.class
). However, when the notification is displayed, the app could be in foreground or in background.
Following Google advice I have the following code
Intent resultIntent = new Intent(this, Event.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(Event.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Here is my Manifest:
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".MainActivity" android:screenOrientation="portrait" android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Event" android:screenOrientation="portrait"></activity>
<activity android:name=".HomeContainer" android:windowSoftInputMode="adjustPan" android:screenOrientation="portrait"></activity>
</application>
When the app is in background
When I open the notification, the Event.class
is created. When I click on the back_button, the MainActivity.class
is created. That's normal and it's what I want.
When the app is in foreground
I'am on EDIT MainActivity.class
HomeContainer.class
. Then I open the notification and so the Event.class
is created. When I click on the back_button, the MainActivity.class
is recreated. I don't want that. Just want to resume()
the EDIT MainActivity.class
Homecontainer.class
in this case.
How can I do ? I searched the different INTENT_FLAGS on Android documentation but nothing seems to be adapted.
Thanks in advance.
As demanded by David Wasser,
Result of : adb shell dumpsys activity activities
ACTIVITY MANAGER ACTIVITIES (dumpsys activity activities)
Display #0 (activities from top to bottom):
Stack #1:
mFullscreen=true
mBounds=null
Task id #3259
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{1d5ebe7 #3259 A=me.APPNAME.app U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a132 mCallingUid=u0a132 mUserSetupComplete=true mCallingPackage=me.APPNAME.app
affinity=me.APPNAME.app
intent={flg=0x18000000 cmp=me.APPNAME.app/.Event}
realActivity=me.APPNAME.app/.Event
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=0
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/3259_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1519781736501 lastActiveTime=1519781736501 (inactive for 5s)
* Hist #0: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
packageName=me.APPNAME.app processName=me.APPNAME.app
launchedFromUid=10132 launchedFromPackage=me.APPNAME.app userId=0
app=ProcessRecord{1e57f14 19075:me.APPNAME.app/u0a132}
Intent { flg=0x18000000 cmp=me.APPNAME.app/.Event (has extras) }
frontOfTask=true task=TaskRecord{1d5ebe7 #3259 A=me.APPNAME.app U=0 StackId=1 sz=1}
taskAffinity=me.APPNAME.app
realActivity=me.APPNAME.app/.Event
baseDir=/data/app/me.APPNAME.app-1/base.apk
dataDir=/data/user/0/me.APPNAME.app
splitDir=[/data/app/me.APPNAME.app-1/split_lib_directories_apk.apk]
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={384dpi} labelRes=0x7f100029 icon=0x7f0d0000 theme=0x7f110006
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff1976d2
launchFailed=false launchCount=1 lastLaunchTime=-5s734ms
haveState=false icicle=null
state=RESUMED stopped=false delayedResume=false finishing=false
keysPaused=false inHistory=true visible=true sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=true lastVisibleTime=-5s9ms
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #3258
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{1a00394 #3258 A=me.APPNAME.app U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a132 mCallingUid=u0a132 mUserSetupComplete=true mCallingPackage=me.APPNAME.app
affinity=me.APPNAME.app
intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=me.APPNAME.app/.MainActivity}
realActivity=me.APPNAME.app/.MainActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=0 mTaskToReturnTo=1
rootWasReset=true mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}]
askedCompatMode=false inRecents=false isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/3258_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1519781736451 lastActiveTime=1519781736451 (inactive for 5s)
* Hist #0: ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}
packageName=me.APPNAME.app processName=me.APPNAME.app
launchedFromUid=10132 launchedFromPackage=me.APPNAME.app userId=0
app=ProcessRecord{1e57f14 19075:me.APPNAME.app/u0a132}
Intent { flg=0x10000 cmp=me.APPNAME.app/.HomeContainer }
frontOfTask=true task=TaskRecord{1a00394 #3258 A=me.APPNAME.app U=0 StackId=1 sz=1}
taskAffinity=me.APPNAME.app
realActivity=me.APPNAME.app/.HomeContainer
baseDir=/data/app/me.APPNAME.app-1/base.apk
dataDir=/data/user/0/me.APPNAME.app
splitDir=[/data/app/me.APPNAME.app-1/split_lib_directories_apk.apk]
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={384dpi} labelRes=0x7f100029 icon=0x7f0d0000 theme=0x7f110006
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff1976d2
launchFailed=false launchCount=0 lastLaunchTime=-35s758ms
haveState=true icicle=Bundle[mParcelledData.dataSize=14188]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=0
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-33s336ms
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #3185
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{ec402a4 #3185 I=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity U=0 StackId=1 sz=1}
userId=0 effectiveUid=u0a43 mCallingUid=u0a43 mUserSetupComplete=true mCallingPackage=com.google.android.googlequicksearchbox
intent={flg=0x10808000 cmp=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity}
realActivity=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity
autoRemoveRecents=false isPersistable=false numFullscreen=1 taskType=0 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{6387e63 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity t3185}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/3185_task_thumbnail.png
stackId=1
hasBeenVisible=true mResizeMode=RESIZE_MODE_UNRESIZEABLE isResizeable=false firstActiveTime=1519761319987 lastActiveTime=1519761319987 (inactive for 20422s)
* Hist #0: ActivityRecord{6387e63 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity t3185}
packageName=com.google.android.googlequicksearchbox processName=com.google.android.googlequicksearchbox:search
launchedFromUid=10043 launchedFromPackage=com.google.android.googlequicksearchbox userId=0
app=ProcessRecord{54c0ce7 16531:com.google.android.googlequicksearchbox:search/u0a43}
Intent { flg=0x10808000 cmp=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity (has extras) }
frontOfTask=true task=TaskRecord{ec402a4 #3185 I=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity U=0 StackId=1 sz=1}
taskAffinity=null
realActivity=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity
baseDir=/data/app/com.google.android.googlequicksearchbox-1/base.apk
dataDir=/data/user/0/com.google.android.googlequicksearchbox
stateNotNeeded=false componentSpecified=true mActivityType=0
compat={384dpi} labelRes=0x7f140c2b icon=0x7f02047c theme=0x7f1501ec
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff4285f4
launchFailed=false launchCount=0 lastLaunchTime=-5h31m17s606ms
haveState=true icicle=Bundle[mParcelledData.dataSize=5644]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=2
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=APPLICATION_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-5h9m27s823ms
resizeMode=RESIZE_MODE_UNRESIZEABLE
Running activities (most recent first):
TaskRecord{1d5ebe7 #3259 A=me.APPNAME.app U=0 StackId=1 sz=1}
Run #2: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
TaskRecord{1a00394 #3258 A=me.APPNAME.app U=0 StackId=1 sz=1}
Run #1: ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}
TaskRecord{ec402a4 #3185 I=com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity U=0 StackId=1 sz=1}
Run #0: ActivityRecord{6387e63 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.staticplugins.opa.OpaActivity t3185}
mResumedActivity: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
mLastPausedActivity: ActivityRecord{e5f96d1 u0 me.APPNAME.app/.HomeContainer t3258}
Stack #0:
mFullscreen=true
mBounds=null
Task id #2488
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{f9fd3c2 #2488 I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity U=0 StackId=0 sz=1}
userId=0 effectiveUid=u0a83 mCallingUid=u0a29 mUserSetupComplete=true mCallingPackage=com.android.systemui
intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000100 cmp=com.google.android.apps.nexuslauncher/.NexusLauncherActivity}
realActivity=com.google.android.apps.nexuslauncher/.NexusLauncherActivity
autoRemoveRecents=false isPersistable=true numFullscreen=1 taskType=1 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/2488_task_thumbnail.png
stackId=0
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=false firstActiveTime=1519781704569 lastActiveTime=1519781704569 (inactive for 37s)
* Hist #0: ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}
packageName=com.google.android.apps.nexuslauncher processName=com.google.android.apps.nexuslauncher
launchedFromUid=0 launchedFromPackage=null userId=0
app=ProcessRecord{275537b 13195:com.google.android.apps.nexuslauncher/u0a83}
Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000100 cmp=com.google.android.apps.nexuslauncher/.NexusLauncherActivity }
frontOfTask=true task=TaskRecord{f9fd3c2 #2488 I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity U=0 StackId=0 sz=1}
taskAffinity=null
realActivity=com.google.android.apps.nexuslauncher/.NexusLauncherActivity
baseDir=/data/app/com.google.android.apps.nexuslauncher-1/base.apk
dataDir=/data/user/0/com.google.android.apps.nexuslauncher
stateNotNeeded=true componentSpecified=false mActivityType=1
compat={384dpi} labelRes=0x7f0c008c icon=0x7f02002b theme=0x7f120002
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.11}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=fff5f5f5
launchFailed=false launchCount=0 lastLaunchTime=-3d3h30m22s60ms
haveState=true icicle=Bundle[mParcelledData.dataSize=5208]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=2
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=HOME_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-38s713ms
connections=[]
resizeMode=RESIZE_MODE_RESIZEABLE
Task id #2490
mFullscreen=true
mBounds=null
mMinWidth=-1
mMinHeight=-1
mLastNonFullscreenBounds=null
* TaskRecord{39e75d3 #2490 A=com.android.systemui U=0 StackId=0 sz=1}
userId=0 effectiveUid=u0a29 mCallingUid=u0a29 mUserSetupComplete=true mCallingPackage=com.android.systemui
affinity=com.android.systemui
intent={flg=0x10804000 cmp=com.android.systemui/.recents.RecentsActivity bnds=[38,1727][1042,3206]}
realActivity=com.android.systemui/.recents.RecentsActivity
autoRemoveRecents=false isPersistable=false numFullscreen=1 taskType=2 mTaskToReturnTo=1
rootWasReset=false mNeverRelinquishIdentity=true mReuseTask=false mLockTaskAuth=LOCK_TASK_AUTH_PINNABLE
Activities=[ActivityRecord{52e27f8 u0 com.android.systemui/.recents.RecentsActivity t2490}]
askedCompatMode=false inRecents=true isAvailable=true
lastThumbnail=null lastThumbnailFile=/data/system_ce/0/recent_images/2490_task_thumbnail.png
stackId=0
hasBeenVisible=true mResizeMode=RESIZE_MODE_RESIZEABLE isResizeable=true firstActiveTime=1519781703116 lastActiveTime=1519781703116 (inactive for 39s)
* Hist #0: ActivityRecord{52e27f8 u0 com.android.systemui/.recents.RecentsActivity t2490}
packageName=com.android.systemui processName=com.android.systemui
launchedFromUid=10029 launchedFromPackage=com.android.systemui userId=0
app=ProcessRecord{cd948d2 12010:com.android.systemui/u0a29}
Intent { flg=0x10804000 cmp=com.android.systemui/.recents.RecentsActivity bnds=[38,1727][1042,3206] }
frontOfTask=true task=TaskRecord{39e75d3 #2490 A=com.android.systemui U=0 StackId=0 sz=1}
taskAffinity=com.android.systemui
realActivity=com.android.systemui/.recents.RecentsActivity
baseDir=/system/priv-app/SystemUI/SystemUI.apk
dataDir=/data/user_de/0/com.android.systemui
stateNotNeeded=true componentSpecified=true mActivityType=2
compat={384dpi} labelRes=0x7f0d02b9 icon=0x7f020175 theme=0x7f1301ea
config={1.0 208mcc15mnc [fr_FR] ldltr sw449dp w449dp h775dp 384dpi nrml long port finger -keyb/v/h -nav/h s.7}
taskConfigOverride={1.0 ?mcc?mnc ?localeList ?layoutDir ?swdp ?wdp ?hdp ?density ?lsize ?long ?orien ?uimode ?night ?touch ?keyb/?/? ?nav/?}
taskDescription: iconFilename=null label="null" color=ff212121
launchFailed=false launchCount=0 lastLaunchTime=-3d3h11m55s774ms
haveState=true icicle=Bundle[mParcelledData.dataSize=500]
state=STOPPED stopped=true delayedResume=false finishing=false
keysPaused=false inHistory=true visible=false sleeping=false idle=true mStartingWindowState=STARTING_WINDOW_NOT_SHOWN
fullscreen=true noDisplay=false immersive=false launchMode=3
frozenBeforeDestroy=false forceNewConfig=false
mActivityType=RECENTS_ACTIVITY_TYPE
waitingVisible=false nowVisible=false lastVisibleTime=-41s902ms
resizeMode=RESIZE_MODE_RESIZEABLE
Running activities (most recent first):
TaskRecord{f9fd3c2 #2488 I=com.google.android.apps.nexuslauncher/.NexusLauncherActivity U=0 StackId=0 sz=1}
Run #1: ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}
TaskRecord{39e75d3 #2490 A=com.android.systemui U=0 StackId=0 sz=1}
Run #0: ActivityRecord{52e27f8 u0 com.android.systemui/.recents.RecentsActivity t2490}
mLastPausedActivity: ActivityRecord{871cda8 u0 com.google.android.apps.nexuslauncher/.NexusLauncherActivity t2488}
mFocusedActivity: ActivityRecord{d8edb34 u0 me.APPNAME.app/.Event t3259}
mFocusedStack=ActivityStack{690a510 stackId=1, 10 tasks}
mLastFocusedStack=ActivityStack{690a510 stackId=1, 10 tasks}
mSleepTimeout=false
mCurTaskIdForUser={0=3259}
mUserStackInFront={}
mActivityContainers={0=ActivtyContainer{0}A, 1=ActivtyContainer{1}A}
mLockTaskModeState=NONE mLockTaskPackages (userId:packages)=
0:[]
mLockTaskModeTasks[]
The updated code I tried (following the advice of David Wasser) :
Intent resultIntent = new Intent(this, Event.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
/** remove uninteresting lines */
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(getApplicationContext(), channel_id);
notificationBuilder.setContentIntent(contentIntent);
/** other parameters as title, icons etc */
notificationManager.notify(_id, notificationBuilder.build());
Do not use Intent.FLAG_ACTIVITY_MULTIPLE_TASK
. This is very special and will get you into trouble and is not needed and isn't what you want.
Do not use TaskStackBuilder
. This constructs an Intent
that will clear out any existing task when the Notification
is opened. This is why, when the app is running, you get the same behaviour as when the app is not running.
Just do this:
Intent resultIntent = new Intent(this, Event.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
This will launch Event
into an existing task (if the app is running), or create a new task and launch Event
into that task. If you want MainActivity
to be started if the user presses the BACK key (and the app was not already running), do this:
In Event
override onBackPressed()
:
@Override
public void onBackPressed() {
if (isTaskRoot()) {
// This Activity is the root Activity, so start `MainActivity`
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
} else {
// Just use the default implementation
super.onBackPressed();
}
}