Android - keep activity in background when clicking local notification - android

My app receives FCM message and sends a local notification. When the app is running with other activities on top of the MainActivity, I don't want the notification to start a new MainActivity or bring it to the foreground. I need it to stay at bottom of the stack, check the data in the local notification and show a dialog to the user. Is there any way to do this?
Or, any way to make whatever the activity on top of the stack to handle the local notification?

Well you can achieve this by creating a base activity, which you will inherit in all of your application's activities. When you receive an event, you simply check if the activity that is at the top of the stack is an instance of your base activity or not.
If it is, you can create a dialog in the base activity with the data that you received in the event.
Hope it helps.

I sent a broadcast to my app instead of starting activity when local notification is clicked. The broadcast receiver checks for the top activity and performs some tasks accordingly.

Related

Android activity, service, notification and application launcher

I need your help because I want to write an Android app which works like this: when I tap the app launcher, I show an activity in which the user has to insert some data, like his name ecc. These data are passed to a background service through an intent and when the service starts I show a notification. When I click on the notification I show another activity in which the user can press a button to stop the service. Everything works fine but I would like that, if I tap again the app launcher but the service is active, the user didn't see the first activity (the one in which he has to insert the data), but the last one in which there is the button to press to stop the service. I don't have a clue of how to do. Can you help me please? Thanks.
Do you really need two Activities for these use cases? You can have just one Activity and two Fragments, one for starting the Service, and one for stopping. In your Activity's onCreate() method check if the Service is running and inflate the right Fragment dynamically.

Launching dialog activity from services caused 'MainActivity' to also launch

I am designing an app that is used for emergency alerts. The alerts come from a server and a connection to that server is maintained in service.
If the service receives an emergency request from the server it checks to see if a specific activity is open. If it is it lets it know an emergency has been triggered and the activity launches a dialog activity with some options. It then handles results from this new dialog activity.
However, if the service notes that the activity is NOT open I want it to launch the dialog anyway. I know that this isn't good practise but because of the importance of this emergency I don't want to rely on Notifications (which are already in use if the activity is closed to let the user know that the app is still listening for emergencies).
What currently happens is that the below code is executed in the service and the dialog launches. However, the 'main' activity (the only other activity in the app) also opens behind the dialog. What I really want to happen is that either...
1) The service launches the main activity which then opens the dialog so that I can easily capture the results.
2) The service launches only the dialog activity and I use a broadcast receiver to capture results from this activity.
1 would use the mechanics that already exist for capturing results from an activity. However I don't like the idea of chaining the activities together in this way.
2 means I can ignore the main activity all together (because I don't really need it in this instance) but seems more of a get around.
What I am really asking is two things. What is best practise given my circumstances and how do i achieve number 2? Here is the launch code in my service. Notification in this code is referring to the dialog activity that will open.
if (MainActivity.isActivityInUI) {
//Dealt with by activity
sendMessageAlert(message);
} else {
//Launch dialog directly from service
Intent notification = new Intent(this,
EmergencyNotificationActivity.class);
Bundle args = new Bundle();
args.putString(MobileMessage.EXTRA_LOCATION_NAME,
message.locationName);
args.putString(MobileMessage.EXTRA_ID,
String.valueOf(message.id));
args.putDouble(MobileMessage.EXTRA_LATITUDE,
Double.valueOf(message.latitude));
args.putDouble(MobileMessage.EXTRA_LONGITUDE,
Double.valueOf(message.longitude));
//and the flag to let the notification know this is from a service...
args.putBoolean(EXTRA_FROM_SERVICE, true);
notification.putExtras(args);
//add flag because this is being called from outside of an activity
notification.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |ActivityInfo.LAUNCH_SINGLE_INSTANCE);
startActivity(notification);
I think instead of trying to show a dialog without visibly showing an Activity, you should consider launching an Activity that is themed like a Dialog. Just apply the following theme: http://developer.android.com/reference/android/R.style.html#Theme_Dialog (or similar themes) to your EmergencyNotificationActivity. You probably would have to tweak your class to behave like a dialog instead of launching one (which I am assuming is what you're doing currently).
This method would also allow you to not have to check if an Activity already exists.

How to Bring Android Activity to the Foreground and Detect if its in background?

I have a callback inside an Activity which is called by a remote service. I fire a Toast when it gets called so, i know its getting called but its in the background, so I need a way from within the activity to make it go to the foreground. How can this be done. Also detect if I am in Foreground or not. How can this be done?
Using the Android Lifecycle you can detect if your Activity is in the background. To return your Activity to the foreground use a Notification in the Status Bar. You can forcibly move your Activity to the foreground by firing an Intent but that can cause frustration and annoyance to the user.
Just call startActivity with the right Intent.
Edit: Forgot to say, that it'a a very confusing behaviour, if an Activity brings itself to the front. The user could do something else and be disturbed. Set a notification instead.

Sending task to background and Bring it to foreground

I have this requirement to send my application background and then bring it to foreground on some key capture intents (not from application launcher offcourse) So How can I send the current tasks to background and bring the same to foreground ?
Use moveTaskToBack() to send the activity in the background and still running if the user presses the back key.
see :Activity for the way on how to do this. its quite simple.
so in order to do this you will also need to override the onBackPressed() method or onKeyPressed() and call this method if the back button was pressed (dont forget to return true on the back pressed methods so android is aware that you consumed the event and doesnt finish the activity).
For returning to this activity that you have moved to the background you can post a notification with a pending intent to launch it back and that will automatically bring the activity to foreground.
Hope this helps.
To send you application to background you should call moveTaskToBack() from your Activity class. When your Activity gets new intent (btw. the onNewIntent() method from your Activity will be called) your Activity gets into foreground by system (you don't have to do anything).
What do you mean by "background?" Activities are stacked one upon another as you create new Activities, then accessed in reverse order using the device's back button. Think of the push() and pop() methods, it's the same paradigm. Applications that need to have code running non-interactively should extend android.app.Service, but beware that you can do some real damage implementing a service. Rogue processes can drain battery life and reduce UI responsiveness.
I solve all the problem pertaining to notification start with fresh activity after moveTaskToBack(true) when back key is pressed by
adding to manifest android:launchMode="SingleTask" android:clearTaskOnLaunch="true"in the activity xml markup section

how to send data to multiple activities in controlled manner in Android?

I am working on an android application, where there are 4 activities. Activity1 is main activity and there are different buttons on that activity which can open other activities. Other 3 activities can also open each other. There is a thread running in Activity1 which returns some counter. I need to show that counter on all activities.
At an specific time, the thread doesn't know which activity is at top. What is the right way to control this scenario, such that thread output should update on all activities, no matter which one is at top?
I would have that counter on a Service and I would send updates through broadcast intent.
Each Activity will register a listener to that intent on the onStart method and unregister it on onPause. This way the update will reach only the Activity that is on screen.

Categories

Resources