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

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.

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.

How can I achieve the following start activity intent mode

I have a broadcast receiver launching a transparent activity while my application is either not launched at all or is in the background. This transparent activity behaves like a pop-up-box that appears on top of the android OS (thus transparent).
If my application is not launched at the moment (exists neither in foreground nor background), when the broadcast receiver triggers the start intent action everything behaves as it should and my transparent activity appears on top of the android OS.
I have another scenario where my application is paused - is in the background. From this state, if my broadcast receiver is triggering the action to start the transparent activity it does so in a way that it moves my last activity on the screen and my 'transparent activity' on top of it thus not being transparent anymore, I can see through it the contents of the activity behind it (from my application). This behavior is not desired and I wan't to change it in a way that when the pop up activity is started, the activity that was before be invisible. How can I do this, how can I hide all other activities from my app in the stack and have only the last one visible !?
This is how I send the intent:
Intent intent = new Intent(getApplicationContext(), PopTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
use this
Intent intent = new Intent(getApplicationContext(), PopTestActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Android Pending intent with current application state

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.

Can I start both an activity and a Service through a PendingIntent in the notification area

I cannot see how to start both when user click a Notification.
I think the answer is yes, but not directly.
You can specify an extra to the intent whether to start the service and parse this extra in when your activity gets launched. The activity can start the service accordingly if needed.
If you don't want to mess with your current activity you can create another activity "A" that starts your activity "B" and your service and start the activity A from the pending intent

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