Since i didnt get any answer after searching on this, i stopped trying but just some days before i started trying again and i found out the SOLUTION. :) I am putting it here.
But first of all i just wanted to redefine my problem and it was :: "Getting background task to foreground from status notification bar" i.e., When i put my app to background by pressing home button and then i make it appear to foreground, all activities should come to foreground in the same order as they are in stack of the app.
For this, i needed a Pending Intent that whenever fired brings my app to foreground.
Here, as my app starts, a notification is put on the status bar that remains there till my app runs on device.
So here is the code i tried::
NotificationManager nm = (NotificationManager) getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = new Notification();
notification.flag = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
Intent nIntent = new Intent();
final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
final List<RecentTaskInfo> recentTaskInfos = am.getRecentTasks(1024,0);
String myPkgNm = getPackageName();
if (!recentTaskInfos.isEmpty()) {
RecentTaskInfo recentTaskInfo;
final int size = recentTaskInfo.size();
for (int i=0;i<size;i++) {
recentTaskInfo = recentTaskInfos.get(i);
if (recentTaskInfo.baseIntent.getComponent().getPackageName().equals(myPkgNm)) {
nIntent = recentTaskInfo.baseIntent;
nIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
}
}
PendingIntent pi = PendingIntent.getActivity(this, 0, nIntent, 0);
notification.setLatestEventInfo(getContext(), "some Title", "some text", pi);
nm.notify(0,notification);
The main key here is the baseIntent, whose documentation says:
The original Intent used to launch the task. You can use this Intent to re-launch
the task (if it is no longer running) or bring the current task to the front.
Documentation of FLAG_ACTIVITY_NEW_TASK says: When using this flag, if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in.
I used it first but it doesnt work as it says and there are many questions on this..
Anyways.. till now, i found this way good for bringing task to front just as icon on the home screen of android does.
But please tell me cons of this way so that i can improve.
:)