Unable to create back stack for activities - android

I am receiving a notification and i want to create a custom back stack so the user can navigate through it.But as of now clicking on the notification opens the desired activity but when i press the back button it completely exits the app.
Intent resultIntent = new Intent(this, NotifViewActivity.class);
resultIntent.putExtra(StringHolder.NOTIFICATION_ID, notif.getId());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(HomeActivity.class);
stackBuilder.addParentStack(NotifActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setContentTitle(notif.getTitle())
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(resultPendingIntent);
Manifest File
<activity
android:name=".NotifActivity"
android:parentActivityName=".HomeActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".HomeActivity" />
</activity>
<activity
android:name=".NotifViewActivity"
android:parentActivityName=".NotifActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".NotifActivity" />
</activity>
The way i want it to work is,on click of the notification the user is taken to
NotifViewActivity then when back button is pressed the user is taken to NotifActivity and when back button is pressed again the user is taken to
HomeActivity .Thats the hierarchy i am trying to create,how can i do that?

You should build your task stack that way:
stackBuilder.addParentStack(HomeActivity.class);
stackBuilder.addParentStack(NotifActivity.class);
stackBuilder.addNextIntentWithParentStack(resultIntent);
Or actually because you already specifying activity hierarchy in manifest, you can do it with just one line:
stackBuilder.addNextIntentWithParentStack(resultIntent);
Or another way to archive the same without specifying hierarchy in manifest:
Intent mainActivityIntent = new Intent(this, HomeActivity.class);
Intent notifActivityIntent = new Intent(this, NotifActivity.class);
stackBuilder.addNextIntent(mainActivityIntent);
stackBuilder.addNextIntent(notifActivityIntent);
stackBuilder.addNextIntent(resultIntent);

For anyone who is trying to start those the created activity with TaskStackBuilder, follow #Divers solution and then use taskStackBuilder.startActivities().

try this :
put below code into NotifViewActivity
#Override
public void onBackPressed() {
Intent i = new Intent(this, HomeActivity.class);
i.putExtra("exit", true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
super.onBackPressed();
}

Related

Fired notification from Broadcastreceiver is closing app when backpressed

Well I've an app it has basicly three 3 classes which are 1-MainActivity, 2- DetailActivity and 3-Broadcast. even app was closed broadcastreceiver is working and if it receives something, it fires notification. when I pressed notification it is opening Detail class. until this point there is no problem it is working perfect. but if I press back in DetailActivity. it is directing to me home page of phone. but app should direct me to Main class. after app direct me to home page if I go back to app from background running apps it will start main class I am using docs codes which are:
<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=".DetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
and
int id = 1;
...
Intent resultIntent = new Intent(this, DetailActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack
stackBuilder.addParentStack(DetailActivity.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());
Actualy I found the answer after some try and error,
PendingIntent.FLAG_UPDATE_CURRENT
After changing this line it works fine, also instead of ".this" I am using getApplicationContext(); for context
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_IMMUTABLE
);
as #MeknessiHamida mentioned..you should override the OnBackPressed method and start a intent to launch your main activity.
you just need to override it in the detailsActivity ( as per your question's ) : make an intent that directs you to the main Activity:
#Override public void onBackPressed() {
//Include the code here
return; }

Android navigation up about notification with TaskStackBuilder

I want to start the "ChatActivity" of my app by click Notification, and when i click back button, we will go to the "MainActivity" of my app. I write my code as the Android Notification Guide. My code is follow.
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setAutoCancel(true)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("你有" + unreadCount + "条新的消息")
.setContentText(content)
.setNumber((int) unreadCount)
//.setColor(Color.RED)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.mipmap.icon_notify_bold);
TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
Intent resultIntent = new Intent(context, ChatActivity.class);
resultIntent.putExtra("to", CommonUtils.removeString(conversation.communicator,
ProfileHelper.getProfile().username));
resultIntent.putExtra("conversationId", conversation.id);
taskStackBuilder.addParentStack(ChatActivity.class);
taskStackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(resultPendingIntent);
manager.notify(NOTIFY_ID_MESSAGE, builder.build());
My manifest xml is like this:
<activity
android:name=".ui.HomeActivity"
android:label="#string/homepage"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ui.ChatActivity"
android:label="#string/back"
android:launchMode="singleTask"
android:parentActivityName=".ui.HomeActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ui.HomeActivity" />
</activity>
My problem is when I press back button in ChatActivity, the HomeActivity will be recreated even if I already started my app. If I have started my app and stay at the HomeActivity page, I think it should not created again. How to avoid recreate HomeActivity?
Any help is grateful.
On ChatActivity class add the following code snippet
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}

TaskStackBuilder not working

I am trying to create synthetic stack but it is not working.
Here is my code.
Intent resultIntent = new Intent(context, NotificationListScreen.class);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack
stackBuilder.addParentStack(NotificationListScreen.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);
// Puts the PendingIntent into the notification builder
builder.setContentIntent(resultPendingIntent);
// Kills the notification when user clicks on it
builder.setAutoCancel(true);
Manifest Entry
<activity
android:name=".modules.notification.NotificationListScreen"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/title_activity_notification_list_screen"
android:parentActivityName=".modules.splash.SplashScreen">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".modules.splash.SplashScreen"/>>
I went through this Android - Build a notification, TaskStackBuilder.addParentStack not working for trouble shooting.

Open Application home page after dismissing notification

Upon pressing the back button, after opening the notification, the user is taken back to the Home screen instead of going back to the main page of the application. (Using a Samsung S5 with Android 5.0)
The notification is built and shown as follows:
NotificationManager mNotifyMgr =
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent resultIntent = new Intent(GcmMessageHandler.this, ListViewItemDetailActivity.class);
Bundle b = new Bundle();
//... put some data
resultIntent.putExtras(b);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(GcmMessageHandler.this);
stackBuilder.addParentStack(ListViewItemDetailActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(GcmMessageHandler.this)
.setContentTitle("Notification")
.setSmallIcon(R.drawable.common_signin_btn_icon_dark)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentIntent(resultPendingIntent)
.setPriority(0)
.setContentText(title);
mNotifyMgr.notify(mNotificationId++, mBuilder.build());
Also in the Manifest file, i have set the parentActivity as follows
<activity
android:name=".ListViewItemDetailActivity"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
The simplest thing that you can do is pass the bool variable form your notification e.g**(_Is_Coming_From_notification)** and in your ListViewItemDetailActivity activtiy get that variable and based on that if user go back open your app home page.
below is some code for your reference.
resultIntent.putExtra("is_Comming_Form_Notification", true);
get that in your activity.
boolean _Is_Comming_From_Notification = intent.getBooleanExtra("is_Comming_Form_Notification", false);
and in your BackPressed method
#Override
public void onBackPressed() {
if (_Is_Comming_From_Notification ) {
Intent intent = new Intent(this, App_Home_Page.class);
startActivity(intent);
}
super.onBackPressed();
}

Android TaskStackBuilder and toolbar icon blink

My code of notification:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getResources().getString(R.string.app_name))
.setContentText("test")
.setAutoCancel(true)
.setTicker("Powiadomienie: test");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
Intent ia = new Intent(this, ActivityA.class);
Intent ib = new Intent(this, ActivityB.class);
Intent ic = new Intent(this, ActivityC.class);
stackBuilder.addNextIntent(ia);
stackBuilder.addNextIntent(ib);
stackBuilder.addNextIntent(ic);
mBuilder.setContentIntent(stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT));
mNotificationManager.notify(1, mBuilder.build());
Order of activity is:
ActivityA -> ActivityB -> ActivityC
When shows ActivityC stack is build from end like in code. On back button show ActivityB and ActivityA.
On video shows what's happen:
http://www.youtube.com/watch?v=YP_-KE-fuCg
When shows last activity (ActivityA) toolbar icon show standard icon and after shows icon of activity. This icon is set in AndroidManifest.xml
<activity android:name="ActivityA" android:icon="#drawable/ic_launcher2"></activity>
<activity android:parentActivityName=".ActivityA" android:name="ActivityB" android:icon="#drawable/ic_launcher2">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ActivityA" />
</activity>
<activity android:parentActivityName=".ActivityB" android:name="ActivityC" android:icon="#drawable/ic_launcher2">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ActivityB" />
</activity>
I cant find answer how to fix it. I dont want to blink icon. Someone have solution of this problem?
Here is sample code: https://dl.dropboxusercontent.com/u/6099319/NotificationTest.zip

Categories

Resources