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; }
Related
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();
}
I use the GoogleTransitionIntentService to show a notification if a user enter a Geofence, but the notification recreate MainActivity.class. I want to resume this activity.
https://github.com/googlesamples/android-play-location/tree/master/Geofencing
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
PendingIntent notificationPendingIntent =
PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setColor(Color.RED)
.setContentTitle(notificationDetails)
.setContentText("Test")
.setContentIntent(notificationPendingIntent);
builder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
Manifest:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You have to use launchmode as singleTop inside your menifest for the given activity.
android:launchMode = "singleTop"
Try adding android:launchMode="singleTop" for MainActivity in the manifest.
You can add flags like
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
and set
android:launchMode = "singleTop"
to activity in manifest.
From GoogleDoc
If the activity has launch mode singleTop (or the up intent contains FLAG_ACTIVITY_CLEAR_TOP), the parent is brought to the top of the stack, and its state is preserved. The intent is received by the activity's onNewIntent() method. If the activity has launch mode standard (and the up intent does not contain FLAG_ACTIVITY_CLEAR_TOP), the current activity and its parent are both popped off the stack, and a new instance of the parent activity is created to receive the navigation intent.
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;
}
I'm having an activity with some EditTexts and an Ongoing Notification.
After filling into EditTexts, I come back Home screen by pressing Home button (my app is running in background). All I want is to come back my activity with filled EditTexts (not to create a new one) when I click the Ongoing Notification.
I have tried this
How should i do from notification back to activity without new intent
And this
Notification click: activity already open
They don't work at all !!!
Below is my code snippet
ProtocolMonitorActivity.java
Intent resultIntent = new Intent(this, ProtocolMonitorActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ProtocolMonitorActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.noti_icon)
.setContentTitle("Protocol Monitor App")
.setContentText("Service is running")
.setOngoing(true);
notiBuilder.setContentIntent(resultPendingIntent);
notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(notiId, notiBuilder.build());
Manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".ProtocolMonitorActivity"
android:label="#string/app_name"
android:process="com.android.phone"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEVELOPMENT_PREFERENCE" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ProtocolMonitorActivity" />
</activity>
</application>
Does someone have any idea on this?
Thank you so so much!!!
You cannot do this using TaskStackBuilder. The behaviour of TaskStackBuilder is that it always clears the task (recreating any activites).
You just need a "launch Intent" to bring your task to the foreground in whatever state it happens to be in. There's 2 ways to do this:
Varient 1:
final Intent notificationIntent = new Intent(this, ProtocolMonitorActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Variant 2:
final Intent notificationIntent =
PackageManager.getLaunchIntentForPackage(getPackageName());
Then do:
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notiBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.noti_icon)
.setContentTitle("Protocol Monitor App")
.setContentText("Service is running")
.setOngoing(true);
notiBuilder.setContentIntent(resultPendingIntent);
notiManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notiManager.notify(notiId, notiBuilder.build());
Just insert this line
resultIntent.setAction(Intent.ACTION_MAIN);
set android:launchMode="singleTop" in your activity tag in menifest inside required activity.
The following is my code that configures notification bar.
PendingIntent pi = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent(getApplicationContext(), AudioActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_star)
.setContentTitle("My app")
.setContentText("works")
.setContentIntent(pi);
mNotificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE
);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
At runtime, I first show AudioActivity at front-ground, then I open the notification bar that shows a message like "My app works". Clicking on the message opens AudioActivity again.
But it seems that the system calls the onCreate method of AudioActivity when I click notification bar. And before that it does not call the onDestroy of the existing AudioActivity. I was wondering if I have two AudioActivity instances running, and how I can manage them. What I expected is it should have worked as how activities are switched and onStart is used rather onCreate.
Thanks.
The best solution for you is to make it call onNewIntent and reuse the existing activity
In the called activity:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if ("action.action.myactionstring".equals(intent.getAction())) {
finish();
}
}
In the called activities mainfest entry
<activity android:name=".MyNotifiedActivity" >
<intent-filter>
<action android:name="action.action.myactionstring" />
...
</intent-filter>
</activity>
When creating the pending intent
Intent myIntent = new Intent("action string");
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pi = PendingIntent.getActivity(
getApplicationContext(),
0,
myIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
The Intent.FLAG_ACTIVITY_SINGLE_TOP flag tells android to reuse the existing instance of the activity (if it exists). onNewIntent is called instead of onCreate, if it isnt already running, the it calls onCreate as usual
#Nick Cardoso: Here is the changed code. I think it works in the way that it does not call onCreate. I was only wondering why it does not call onNewIntent as you expected. Thanks.
PendingIntent pi = PendingIntent.getActivity(
getApplicationContext(),
0,
new Intent("com.example.myapp.ACTION_NOTIFICATION"),
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_action_star)
.setContentTitle("My app")
.setContentText("works")
.setContentIntent(pi);
mNotificationManager = (NotificationManager) getSystemService(
Context.NOTIFICATION_SERVICE
);
mNotificationManager.notify(NOTIFICATION_ID, builder.build());
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ourradio.AudioActivity"
android:label="#string/title_activity_audio"
android:parentActivityName="com.example.ourradio.FeedActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.ourradio.FeedActivity" />
<intent-filter>
<action
android:name="com.example.myapp.ACTION_NOTIFICATION" />
</intent-filter>
</activity>
<service
android:name="com.example.ourradio.AudioService"
android:label="#string/label_service_audio" >
</service>
</application>
#Joe C , a simpler solution maybe add the SINGLE_TOP flag in the androidManifest as a launchMode in the activity like this:
<activity
android:name="com.example.ourradio.AudioActivity"
android:label="#string/title_activity_audio"
android:parentActivityName="com.example.ourradio.FeedActivity"
android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
you can see this link for more information: http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode