Is it possible to return to previous application from where your application is opened? For example I am in application A then I click on notification and open my application B then in my application BI click a button and I get back to application A? Currently I am trying to do that and the only way how I achieved is when my application is running in background or it only works for the first time.
I am using Flutter but these actions are executed in native Android code so I am probably missing something.
For getting back to previous app I am using: moveTaskToBack(true);
For creating a notification my code looks like this:
private void NotifyToAutofill(String uri, String url, NotificationManager notificationManager) {
if (notificationManager == null || isNullOrWhitespace(uri)) {
return;
}
Context context = getApplicationContext();
Intent startIntent = new Intent(context, MainActivity.class);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startIntent.setAction(Intent.ACTION_RUN);
startIntent.putExtra("uri", uri);
startIntent.putExtra("url", url.contains("\u2022")
|| url.contains("\u2731") ? "" : url);
String channelId = "channel-01";
String channelName = "test";
int importance = NotificationManager.IMPORTANCE_LOW;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel mChannel = new NotificationChannel(
channelId, channelName, importance);
notificationManager.createNotificationChannel(mChannel);
}
int color = 0x008000;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, channelId)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("Test Service")
.setContentText("Tap to open application")
.setAutoCancel(true);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(startIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(AutoFillNotificationId, notification);
}
Found the solution. My problem was that i was creating PendingIntent with
stackBuilder.addNextIntent(startIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
Everything worked as intented when I used:
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, startIntent, PendingIntent.FLAG_UPDATE_CURRENT);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.