Issue
I have a normal activity, let's call it A, and it's the first activity presented to the user when opening the app. Activity A has a button that launches an activity, let's call it B, with launchMode
set to singleInstance
in the manifest. Activity B does some processing.
Users can press the home button and open the app again, which will present them with the starting activity, aka activity A. If users click on the button again (in activity A), they will be presented with activity B. In the case, activity B will not be restarted, i.e., onCreate
will not be called, since it is singleInstance
, which is what I want.
I want to make it easier for the users to come back to activity B once it started if they pressed the home button. So, I created an ongoing notification that lets the users bring activity B back. The ongoing notification will be canceled once activity B is finished.
Now to the problem, clicking the ongoing notification recreates activity B again, i.e., the onCreate
is being called again. I don't know why the behavior here is not the same as clicking on the button on activity A. Here is how I created the notification:
Intent notifyIntent = new Intent(mContext, CallActivity.class);
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
mContext, 0, notifyIntent, PendingIntent.FLAG_NO_CREATE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(mContext, CHANNEL_ID)
.setSmallIcon(R.drawable.baseline_call_white_18)
.setContentTitle(title)
.setContentText(text)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(notifyPendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(mContext);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Can anyone tell me why this is not working and how can I get it to work the way I described?
EDIT
Here is a snippet of my manifest:
<application
android:name="com.mnm.caller.IMApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name="com.mnm.caller.activities.SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.mnm.caller.activities.LoginActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/AppTheme.NoTitleBar"
android:screenOrientation="portrait" />
<activity
android:name="com.mnm.caller.activities.HomeActivity"
android:configChanges="orientation|keyboardHidden"
android:theme="@style/AppTheme.NoTitleBar"
android:screenOrientation="portrait" />
<activity
android:name="com.mnm.caller.activities.CallActivity"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleInstance"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoTitleBar" />
<service
android:name="com.mnm.caller.services.IMFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
Solution
In a comment you wrote:
I'm actually developing a calling app. Activity A is the home screen, while B is the calling activity. I want to let the users navigate back to the home screen during a call, so I intentionally and knowingly chose to create two instances of the app in recent apps (you can see the exact behavior in the default phone app on Android)
If this is the case, you probably do want to have 2 separate tasks that you can switch between. To do this, you need to use taskAffinity
. Your CallActivity
should have launch mode of singleTask
or singleInstance
and you should set android:taskAffinity="call"
or something like that. In order not to confuse your users, you should also provide a different android:label
and a different android:icon
for CallActivity
so that when this task appears in the list of recent tasks it looks different than the rest of your app. Make sure that when you launch CallActivity
you also set FLAG_ACTIVITY_NEW_TASK
. You should also set this flag when you build the Notification
.
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.