Notification to restore a task rather than a specific activity?
What you need is just a simple Activity that does nothing. Here is an example:
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Now finish, which will drop the user in to the activity that was at the top
// of the task stack
finish();
}
}
Set up your notification to start this activity. Make sure that in the manifest the task affinity of this activity is the same as the task affinity of the other activities in your application (by default it is, if you haven't explicitly set android:taskAffinity).
When the user selects this notification, if your application is running, then the NotificationActivity will be started on top of the topmost activity in your application's task and that task will be brought to the foreground. When the NotificationActivity finishes, it will simply return the user to the topmost activity in your application (ie: wherever the user left it when it went into the background).
This won't work if your application isn't already running. However, you have 2 options to deal with that:
Make sure the notification isn't present in the notification bar when your application is not running.
In the onCreate() method of the NotificationActivity, check if your application is running, and if it isn't running call startActivity() and launch your application. If you do this, be sure to set the flag Intent.FLAG_ACTIVITY_NEW_TASK when starting the application so that the root activity of the task is not NotificationActivity.
Using TaskStackBuilder in notification recreates an activity
If you just want to use the notification to allow the user to return to the task in whatever state it was in, then you don't need TaskStackBuilder for that.
See:
Notification to restore a task rather than a specific activity?
Resume application and stack from notification
Starting app only if its not currently running
Notification start application else bring activity to front
No one is being able to answer my question. So, I was forced to use a solution which is little dirty.
So we will need one more class NotificationActivity
.
Wheneven a new notification is received from back-end server. We will send pendingIntent to NotificationActivity.
NotificationCompt.Builder = bla bla bla.
Intent intent = new Intent(context, NotificationActivity.class);
intent.setFlags(FLAG_ACTIVITY_CREATE_NEW_TASK);
PendingIntent pIntent = PendingIntent.startIntent(0, intent, 0, someflags);
In NotificationActivity:
public class NotificationActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstances) {
Intent intent = null;
try {
// Call any method from class which is need by Main activity
someclass.somemethod();
// If we are here then it means app is running.
intent = new Intent(context, MainActivity.class);
// Flag it to bring it to front
intent.setFlags(FLAG_ACTIVITY_BRING_TO_FRONT);
} catch (NullPointerException e ) {
// If we are here then it means that app is closed and
// someclass is garbage collected.
intent = new Intent(context, StartAppActivity.class);
// Flag it to create new task
intent.setFlags(FLAG_ACTIVITY_CREATE_NEW_TASK);
}
// Start intent, and finish this activity
startActivity(intent);
finish();
}
}
This solves all the problems that i have mentioned above. Don´t know if it creates new problems. If you use this then use a method which has low-cost. This activity will be garbage collected as soon as u finish it. Ok! I lie, in the nearest future.
This would have given problem if u had multiple notifications lying, but android lets you put all notifications into a single pendingIntent so the problem will never arise.
I cannot come up with any other problems .. if u can then tell me or wait until it is tested :D
How to restore previous activity by clicking on notification
Figured it out, set the Activity to SingleTop or SingleInstance in Android Manifest, then instead of creating a new activity it just reopen the one still active.
Android notification starting new instance of activity no matter what the launchMode and/or flags are set to
There are 2 ways of doing this:
Notification to restore a task rather than a specific activity?
Resume application and stack from notification
However, you may also be seeing this nasty Android bug.
Trying to restore state of activity upon starting it via a notification
Even though I'm sure I had this combination tested dozens of times, the working combination - in order to restore the state of the WebView - is:
In WebAppInterface.java:
Intent intent= new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0);
In AndroidManifest.xml:
<activity
...
android:launchMode="singleTask" >
Furthermode there was no code needed for onResume()/onNewIntent() in the MainActivity.
Related Topics
Cannot Load Library: Reloc_Library[1285]: Cannot Locate 'Rand'
Blue Dot and Circle Is Not Shown on Mylocation Using Android Fused Location API
How to Make an App's Background Image Repeat
How to Handle Handler Messages When Activity/Fragment Is Paused
Unexpected Value from Nativegetenabledtags: 0
What Is "Android.R.Layout.Simple_List_Item_1"
Sqliteopenhelper Problem with Fully Qualified Db Path Name
Remove Listview Items in Android
How to Request Permissions from a Service in Android Marshmallow
How to Create Edittext Accepts Alphabets Only in Android
How to Display Album Art Using Mediastore.Audio.Albums.Album_Art
Detecting Whether a Headset Is Plugged into an Android Device or Not
Adt Will Not Allow Creation of Android Activity
Java.Lang.Classnotfoundexception on Working App
Java.Lang.Illegalargumentexception: Contains a Path Separator