Android LaunchMode Does Not Work - android

I started an temp activity from notifiation, just show some text messages.Whatever I set launchMode=singleInstance or noHistory=true, The temp activity showed last time will show again when enter from "Recent open". I want temp activity to be shown only if I clicked notification, don't show in the "Recent open". Thank you in advance.
<activity
android:name=".NotifiticationDialog"
android:launchMode="singleInstance"
android:noHistory="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >
Notification notification = new Notification(R.drawable.icon, context.getString(R.string.app_name), System.currentTimeMillis());
Intent intent = new Intent(context, NotifiticationDialog.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent activity = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notifycation.setLatestEventInfo(context, context.getString(R.string.app_name), message, activity);
notifycation.flags = Notification.FLAG_AUTO_CANCEL;
_nm.notify(NotifiticationDialog.ID, text);
EDIT:
#Lalit Poptani, I tried what you suggest, but it is not waht I need. After I click temp activity with android:excludeFromRecents="true", My app disappeared in "Recent".(user can't find it, my all activity is excluded)
EDIT:
Fact: I have 3 tmp activity showing some text just like toast did, 2 were opened from widget and they didn't mixed with app stack. 1 were opend from notification, it always show itself individually from "Recent".
<activity
android:name=".AppwidgetDialog1"
android:launchMode="singleInstance"
android:noHistory="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".AppwidgetDialog2"
android:launchMode="singleInstance"
android:noHistory="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar" />
<activity
android:name=".NotifiticationDialog"
android:excludeFromRecents="true"
android:theme="#android:style/Theme.Translucent.NoTitleBar" >

For removing your Activity from Recent Apps you can use android:excludeFromRecents, so try adding android:excludeFromRecents="true" to your Activity tag.

Related

Bring a singleInstance Activity Back Using a Notification

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>
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.
You need to set FLAG_ACTIVITY_SINGLE_TOP to the Intent instance (in your case notifyIntent) before creating the PendingIntent object.
In other words:
Intent notifyIntent = new Intent(mContext, CallActivity.class).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
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());

Navigating push notification will close intermediate activity when back press navigated activity android

I'm navigating to activity C when clicking push notification. Where activity A is my home screen and I'm currently in activity B from activity A while receiving a push notification. Consider now I'm receiving a push notification and clicking the received notification. After clicking push notification activity C gets loaded. Then I'm back pressing in my app this will close the intermediate activity B. But I do not want to close my intermediate activity.
My Pending intent for activity A looks like below,
Intent intent = new Intent(context, A.class);
intent.putExtra(PUSH_MESSAGE, notification);
PendingIntent.getActivity(context, previousId + 1, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
and the manifest file looks like below,
<activity
android:name=".A"
android:configChanges="orientation|keyboard|screenSize"
android:screenOrientation="portrait"
android:launchMode="singleTask"
android:theme="#style/AppTheme.Light.NoActionBar"/>
<activity
android:name=".B"
android:configChanges="orientation"
android:launchMode="singleTop"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.Light.NoActionBar" />
<activity
android:name=".C"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#style/Theme.Overlay"/>
Also i'm launching activity B from activity A like below,
Intent intent = new Intent(getContext(), B.class);
getContext().startActivity(intent);
and launching Activity C while clicking received push notification like below,
Intent intent = new Intent(context, C.class);
intent.putExtra(C.IS_FROM_PUSH_NOTIFICATION, true);
((Activity) context).startActivityForResult(intent, REQUEST_CODE_REFRESH);
Could you Please suggest me any idea to do this?
Make following changes in manifest.xml
<activity
android:parentActivityName=".B"
android:name=".C"
android:configChanges="orientation|keyboardHidden|screenSize"
android:theme="#style/Theme.Overlay"/>

Android notification Reorder to front doesn't work. Copied from APP that it has worked in

I've copied a code from dummy project testing Services and Notifications in which reoder to font worked like a charm.
Here is the code for notification (pretty same as in tutorials)
NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle("Request awaiting")
.setContentText("There is a service request awaiting for Your reaction");
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.setAction(Intent.ACTION_MAIN);
resultIntent.addCategory(Intent.CATEGORY_LAUNCHER);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);//stackBuilder.getPendingIntent(0, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
nBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = nBuilder.build();
mNotificationManager.notify(1, notification);
Manifest looks like :
<application android:allowBackup="true" android:icon="#drawable/ld"
android:label="#string/app_name" android:theme="#style/AppTheme">
<activity
android:name="com.iraasta.cloudcab.driver.MainActivity"
android:launchMode="singleTop"
android:label="#string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
It looks exactly same in my other app, in which it perfectly brings app to front like when selecting from the home screen.
But in this one it calls OnCreate every time and loses whole state.
To bring your application to the front (if it is in the background) in whatever state it is in, or to launch your app (if it isn't aleady running), you need to launch your "root activity" (the one with ACTION=MAIN and CATEGORY=LAUNCHER) and set the flag Intent.FLAG_ACTIVITY_NEW_TASK. This will launch the activity in a new task (if your app isn't already running), or bring the existing task to the foreground (if your app is already running).
Since you are doing this from a Notification, you should be setting Intent.FLAG_ACTIVITY_NEW_TASK in the Intent you pass to the Notification. However, Android is nice enough to set this flag for you if you forget to do it.
I'm not exactly sure why setting Intent.FLAG_ACTIVITY_REORDER_TO_FRONT caused your problem, but I can imagine that this somehow confuses Android.
Not using any flag fixed it.
To people looking at this post in future.
If Your app uses single activity and You want to bring it to front after clicking the Notification just leave flags as integer 0.
It fixed my problem.
Thanks for suggestions
Bounty goes to the person that will explain why is this flag ruining everything.
Setting android:launchMode="singleTask" in a manifest for an activity you wanna bring to the front works for me

Open Activity with backwards to MainActivity in my app

I'm stuck with some activity's flow issue. The desired behaviour is the following:
From time to time, the user receives a notification. When this notification is clicked, a new Activity is opened with some information in it. In this Activity, there's a button whose purpose is to redirect the user to another Activity where more detailed information is showed. When the user is in the details Activity and presses the back button (or the back button in the ActionBar) this one is closed and the Main Activity is showed (this one is different from the one I mentioned in first place).
Everything works fine except from the last part. When the user presses the back button the application is closed and it is showed the Home Screen. Why is that happening?
Here is my AndroidManifest.xml:
<activity
android:name=".MainActivity">
</activity>
<activity
android:name=".DetailActivity"
android:label="#string/title_detail_activity"
android:parentActivityName="solar.panik.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="solar.panik.MainActivity" />
</activity>
<activity
android:name=".NotificationActivity"
android:theme="#style/NotificationActivity"
android:excludeFromRecents="true">
</activity>
Here is the onClick code for the button that starts the DetailActivity from the NotificationActivity:
Intent intent = new Intent(NotificationActivity.this, DetailActivity.class);
startActivity(intent);
finish();
Thanks in advance
When you start your app from something other than the launcher, you'll need to pass the back stack with your intent.
Android tutorial
Scroll down to Create Back Stack When Starting Activity().
So in your case:
// Intent for the activity to open when user selects the notification
Intent detailsIntent = new Intent(this, DetailActivity.class);
// Use TaskStackBuilder to build the back stack and get the PendingIntent
PendingIntent pendingIntent =
TaskStackBuilder.create(this)
// add all of DetailActivity's parents to the stack,
// followed by DetailsActivity itself
.addNextIntentWithParentStack(detailsIntent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(pendingIntent);
Check out this answer.
OLD ANSWER BELOW
Make sure in your details activity that onBackPressed() method isn't overridden (or defined).
If that's not it, try adding this to your manifest and remove your current ".MainActivity" Activity and tags. (Or replace it with this)
<activity
android:name="solar.panik.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to declare it as MAIN, so the Up button knows where to go. "Back" will take you to the next Activity up on the hierarchy.
Hope that helps.
Do this in your detailed activity. The one you click the back button in.
#Override
public void onBackPressed() {
Intent intent = new Intent(DetailActivity.this, MainActivity.class);
startActivity(intent);
finish();
}

Application is not shown in Recent Apps list when opened via PendingIntent

My app offers a widget. User can add the widget to the home screen of his Android OS. When the user taps the widget my app gets opened.
The issue is: when run so, the app is not shown in the Recent Apps list.
How can I make my app to shown in the Recent Apps list regardless of how it was opened?
Here is the intent used to open the app:
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget_contents, pendingIntent);
Here is the declaration of the activity in the app's manifest:
<activity
android:name=".MainActivity"
android:label="#string/title_activity_browse_playlist"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:launchMode="singleTop">
</activity>

Categories

Resources