Issue
I'm getting an unexpected behavior:
- Activity A = Launcher activity
- Activity B = It's started from a notification
When I have closed the app and open it from the notification, the Activity B is started.
So, when I click on HOME button, the app navigates to the Activity A. Then, I push the BACK button and the app goes to background and I get the Android main screen.
This is OK, but if I open the recent apps menu and select again my app, it loads the Activity B instead Activity A as I expect. I don't really know what is happening.
The intent set in the notification builder is:
Intent intent = new Intent(this, ActivityB.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
In Activity B I have:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == android.R.id.home) {
Intent intent = new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Any idea or suggestion?
Solution
I found a solution which fit in what I'm expecting. When I receive a notification I create a new Task Stack with my mainActivity on the top:
Intent intent = new Intent(this, ActivityB.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ActivityB.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Now, I can also remove the overrided check for HOME button in the Activity B:
// This can be now removed
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (id == android.R.id.home) {
Intent intent = new Intent(ActivityB.this, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
Answered By - alvaro torrico
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.