Get the Intent that resumes the activity - android

Activity.getIntent returns the Intent that created the Activity.
Is there any way to get the Intent that resumes the Activity in the case where the Activity goes from onPause to onResume?

The solution is to override Activity.onNewIntent which is called before onResume.

I found out how to do it add this code:
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Now this should be like this:
notification = new Notification(R.drawable.icon,
"Notify", System.currentTimeMillis());
notification.setLatestEventInfo(this, "App name",
"App message", PendingIntent.getActivity(this,
0, new Intent(this, Main.class)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP),
PendingIntent.FLAG_CANCEL_CURRENT));
notification.flags |= Notification.FLAG_ONGOING_EVENT;

Related

FLAG_ACTIVITY_CLEAR_TOP not working from Notification Section

I've got a simple use case. I have activity open like below:
A > B > C
Now, I get a push notification.
I do the following in my GCMIntentService class:
In onMessage,
Intent notificationIntent = new Intent(context, A.class);
PendingIntent contentIntent =
PendingIntent.getActivity(context,0, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
notification.setContentIntent(contentIntent);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
My expectation is that activities B, C should be removed from Activity stack and you should be re-directed to activity A.
What is happening is, however, A gets opened; but when you hit back button I get C, B, A again in that order!!
What is it that I'm doing wrong?
Here are the things I tried:
I've experimented with
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);,
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP, Intent.FLAG_ACTIVITY_SINGLE_TOP);
I've kept a static reference to the last foreground activity and passed that as context.
Nothing seems to work.
Try this. It may help you. Let me know once you have tested it.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(notificationIntent);
try this Tutorial
Intent notificationIntent = new Intent(context, YourActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Set launch mode of your activity to "singleTask":
<activity
android:name=".ActivityA"
android:launchMode="singleTask"
>
And set flags as below:
Intent intent= new Intent(currentActivity, ActivityA.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

Launch An Activity Afresh(Don't Retain State) On A Notification Click

I want to launch an Activity(Afresh, Without any previously retained state) on a notification click. IF this Activity is already in the stack, bring this to top but without any previous state.
So Far i have tried setting Flags to Passed Intent.
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|INTENT.FLAG_ACTIVITY_SINGLE_TOP)
But They Don't serve the purpose. Any suggestions how i could achieve this.
If you remove |Intent.FLAG_ACTIVITY_SINGLE_TOP from your launch this should restart the activity.
Specifying |Intent.FLAG_ACTIVITY_SINGLE_TOP in the launch flags causes the existing instance to be reused. If you remove it the existing instance will be finished and a new instance will be launched.
Here You Go.
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
to open application in anystate.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(LaunchIntent);

Android. Can't clear or replace extras

I lunch activity from notification.
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
notificationIntent.putExtra(GCM_EXTRA_ID, id);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notificationManager.notify(0, notification);
If activity destroyed, intent creates new activity and call in oncreate activity this code
Bundle extras = getIntent().getExtras();
if(extras != null){
if(extras.containsKey(GCMIntentService.GCM_EXTRA_ID))
{
int id = extras.getInt(GCMIntentService.GCM_EXTRA_ID);
Toast.makeText(this, "GCM_EXTRA_ID ACTIVITY ON CREATE:" + Integer.toString(id) , Toast.LENGTH_LONG).show();
dbClassItem = new DBClass(this);
db = dbClassItem.getWritableDatabase();
DBClass.setLocateValue(db, id);
db.close();
getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0);
}
}
If I close activity by back button and start activity again application still see, that intent has extra with key GCMIntentService.GCM_EXTRA_ID (this is a public static string).
How can I clear or replace this extra for next app start. getIntent().removeExtra(), getIntent().getExtras().clear(),getIntent().putExtra(GCMIntentService.GCM_EXTRA_ID, 0) does not work. What can I do ? Activity has android:launchMode ="singleTop" key.
This problem was solved by using onNewIntent instead of using onCrete in Activity.
You can do this
notificationIntent.putExtra(GCM_EXTRA_ID, null);

how to start Phonegap Android application from notification bar

I have a Android app with Google Cloud Message and Phonegap. Send, receive are done.
I try to launch my app from notification bar. But have problem:
This isn't the root activity. Clearing it and returning to the root activity.
How to fix it?
PendingIntent when create notification:
Intent notificationIntent = new Intent(this, PushHandlerActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("pushBundle", extras);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Force main activity load
PackageManager pm = getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
startActivity(launchIntent);
When i click to notification, it run Intent PushHandlerActivity, and PushHandlerActivity force main activity load. When the main activity load it say "This isn't the root activity. Clearing it and returning to the root activity." in LogCat, and my app can't show up :(
Please look for my code. It works absolutely fine for me
Intent notificationIntent = new Intent(context, SplashScreen.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
Did you try to change:
Intent launchIntent = pm.getLaunchIntentForPackage(getApplicationContext().getPackageName());
With
Intent launchIntent = pm.getLaunchIntentForPackage(<PackageNameOfYourMainActivity>);
Than it will start your PhoneGap main activity.
I didn't try this, it's just looks logical to me.

How can i enter in my application through notification?

This is the code of my notification. I want that onClick my application starts. Right now nothing happen
private void CheckNoti(){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
service.this);
notificationBuilder.setContentTitle("Title");
notificationBuilder.setContentText("Context");
notificationBuilder.setTicker("TickerText");
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);
Intent notificationIntent = new Intent(this, service.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
mNotificationManager.notify(1,
notificationBuilder.build());
} }
This is a code inside a class. It's not inside the MainActivity. So i can't do something like:
intent.setClassName("your.package.name", "ActivityToLaunch");
i think because i already doing Intent notificationIntent = new Intent(this, service.class);
To bring your app to the foreground if it is running already you need to set different flags on your intent:
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
For running a specific method you could just pass extra information along with the intent and interpret it in your application to decide which method to run.
See the answer here

Categories

Resources