The best way I can describe my problem is like this:
BroadcastReceiver
).How can I prevent that last step? What I want with the back button is to go back where I was, which is the home screen (the desktop with all the widgets and app icons). My app's main activity was supposed to be running on the background, why was it called with the back button?
In case it's relevant, my code to create a notification goes like this:
public void createNotification(int notifyId, int iconId, String contentTitle, String contentText) {
Intent intent = new Intent(mContext, NewNoteActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(AgendaNotesAdapter.KEY_ROW_ID, (long)notifyId);
PendingIntent contentIntent = PendingIntent.getActivity(mContext, notifyId, intent, 0);
Notification notification = new Notification(iconId, contentTitle, 0);
notification.setLatestEventInfo(mContext, contentTitle, contentText, contentIntent);
mNotificationManager.notify(notifyId, notification);
I tried to add a couple of more flags combinations to intent
but neither of them solved my problem... Suggestions?
For whose who still might need answer. It looks like this is what you want to achieve:
When you start an
Activity
from a notification, you must preserve the user's expected navigation experience. Clicking Back should take the user back through the application's normal work flow to the Home screen, and clicking Recents should show the Activity as a separate task.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse
Your situation is - Setting up a regular activity PendingIntent
See full steps in the link. Basically you need to:
1. Define Activity
hierarchy in AndroidManifest.xml
<activity
android:name=".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>
<activity
android:name=".ResultActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
2. Create a back stack based on the Intent
that starts the Activity
:
...
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(ResultActivity.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
// Gets a PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
...
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(id, builder.build());