Android Pending intent with current application state - android

Say for example i have two activity . Activity A and Activity B. Activity A is my default launcher activity . And sometimes from background i launch activity B .
Now i put separate notification with pending intent which brings the user back to activity A by clicking on those.
But when my activity B is running ,when i press on my application icon ,android takes me to activity B.But when i click on notification,it takes me to Activity A as the pending intent points to Activity A.
how can i set pending intent which will bring activity B on foreground if its running instead of activity A ?
Thanks
NB : setting flag to re order activity wont work because when activity B is running, A is in back stack,so this flag will bring A to front.what i want is when user will click the notification,it will behave just as like the clicking application icon,that is bring activity B in foreground.

Related

How to send app to background after finishing activity which is started from push notification

I have an activity which is called if the app receives a push notification. The activity is started with FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP. The activity, let's call it 'A' shows UI and finishes after a while. In this point have a problem with activity stack.
Scenario:
The app is in the background with another activity 'B'
Then the app receives a push notification and starts Activity A.
After related things done, the app finishes Activity A
Then returns to Activity B and stays in the foreground even the app was in the background before the push notification is received.
After debugging, I figured out that the system calls onResume method of Activity B after finishing Activity A.
How can I do the app keep staying in background if the app started from background? Should I change intent flags of the activity A?
In your case you can achieve this in two ways
1- From manifest file with activity tag android:noHistory="true"
2- From code when you are staring the activity set flags like below
Intent mIntent = new Intent(context, Youractivity.class);
mIntent.setFlags(mIntent.getFlags() | Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(mIntent);
For more information checkout developers link
One other thing you can do is instead of this.finish() in notificationActivity is to use this.finishAffinity();. This will close the app instead coming to foreground.

Reuse current app instance when clicking on Notification [duplicate]

This question already has answers here:
Resume application and stack from notification
(8 answers)
Closed 6 years ago.
Right now I'm using a PendingIntent to launch my desired Activity from the Notification. But I want to know if it's possible to reuse the current app instance.
For example:
Launch app, the Launcher Activity is called HomeActivity
Navigate to SecondActivity
Press the home button
Click on the notification from the app and resume SecondActivity
But if the user didn't navigate to the SecondActivity i want to open the HomeActivity when I click on the Notification. I would be glad for any help regarding this problem :)
But I want to know if it's possible to reuse the current app instance.
Yes, you can reuse if the app is in background. In PendingIntent you will pass an Intent, so in that Intent you should set a flag like intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); so if the app is in background it will just bring activity to front.
According to android docs :
FLAG_ACTIVITY_NEW_TASK
Added in API level 1 int FLAG_ACTIVITY_NEW_TASK If set, this activity
will become the start of a new task on this history stack. A task
(from the activity that started it to the next task activity) defines
an atomic group of activities that the user can move to. Tasks can be
moved to the foreground and background; all of the activities inside
of a particular task always remain in the same order. See Tasks and
Back Stack for more information about tasks.
This flag is generally used by activities that want to present a
"launcher" style behavior: they give the user a list of separate
things that can be done, which otherwise run completely independently
of the activity launching them.
When using this flag, if a task is already running for the activity
you are now starting, then a new activity will not be started;
instead, the current task will simply be brought to the front of the
screen with the state it was last in. See FLAG_ACTIVITY_MULTIPLE_TASK
for a flag to disable this behavior.
This flag can not be used when the caller is requesting a result from
the activity being launched.
Check the docs here
But if the user didn't navigate to the SecondActivity i want to open the HomeActivity.
Why do you launch HomeActivity if your notification intent class you are creating have SecondActivity as the launcher. It will create a new SecondActivity and launches it if it is not in background.
EDIT: If you want to relaunch app where you left off try like this :
final Intent resumeIntent = new Intent(context, YourLauncher.class);
resumeIntent.setAction("android.intent.action.MAIN");
resumeIntent.addCategory("android.intent.category.LAUNCHER"‌​);
final PendingIntent resumePendingIntent = PendingIntent.getActivity(context, 0, resumeIntent, 0);
Here YourLauncher.class is the launcher class name.

Start pending intent instead of main activity, on app icon click

I have a foreground service on my application. When service running, notification will be display with a pending intent(this pending intent is not my main activity). When click on the notification, pending intent will be starts. Its working fine.
Following are my activities
Main activity - LoginActivity.java
Pending intent activity(which displays when click on notification) - HomeActivity.Java
When click on app icon while service is running, I need to launch pending intent activity(HomeActivity.java) instead of main activity(LoginActivity.java)
How could I do that?
You cannot change what the launcher icon points to dynamically at runtime in a reliable fashion.
You are welcome to have it point to an activity set up with Theme.NoDisplay, which then determines what actual activity should display, starts that activity using startActivity(), and then calls finish() to get rid of itself.
Or, have the launcher icon always point to HomeActivity, which has the logic to detect that a login is needed and then starts LoginActivity.

How to keep layout and its data

I need your help, I have 2 activities A and B where A is the main activity.
Now B starts the activities from A using startActivityForResult() and from B when I finish it will go back to activity A.
This works fine but the actual purpose was like the Gmail application when you go back from B to A and then start activity B again from A, then A need to start activity with its last screen as i left it.
For example: from inbox->label->draft in gmail how to achieve this to keep data/layout as it is.
Maybe android:launchMode="singleInstance" on activity A and B will get it done?
How to switch Activity from 2 activities without losing its data and current state of data
Ok I have got solution that when every time start your application that time only activity will be created and view and put in stack so the next time you start again this activity just open from background so the activity will available in stack and just brought to front using start activity
From A to start ActivityB
Intent intent = new Intent(context,ActivityB.class);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Now for start ActivityA from ActivityB
Intent intent = new Intent(getInstance(), ActivityA.class);
intent.addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
It just brought front while activity start and put the current activity into background

Activity not being reused

I have an odd issue with my application.
I start the application and the activity shows ok.
I then press the home key so that the activity goes into the background. I can see the onPause() method being called.
The application's service then creates a notification which shows on the status bar.
I then click on the notification and the activity is shown and I see that the onResume() method is called.
I then press the home key and the activity goes into the background. I can see the onPause() method being called.
If I now start the application by clicking on the applications icon I see that a new instance of the activity is created rather than using the paused instance.
If I press the home key again the new activity goes into the background.
Starting the application by clicking on the applications icon I see another new instance of the activity is created.
Pressing the back button at the point destroys each activity in return.
What I want to happen is that a single instance of the activity be used.
Any ideas?
Just use the same intent filters as android uses when launches the app:
final Intent notificationIntent = new Intent(context, MessageListActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
As the intent you created to open your activity from notification bar is the same as android used for launching your app, the previously opened activity will be shown instead of creating a new one.
You should look at the different launch modes for Android activities. this should help. Launch modes can be set in the androidmanifest.xml file. I think your solution would be to use the 'singleTop' launch mode.

Categories

Resources