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

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.

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.

How to jump to main activity from subchild activity without using Intent?

I have following activities. 1) Main activity 2) Activity 1 3) Activity 2
This is the way Activities is connected together
Main Activity -> Activity 1 -> Activity 2(Create Background Async Task) So "Activity 1" is child activity and "Activity 2" is a subchild activity. And creating Background Async task from "Activity 2"
So Let's say I am currently on "Activity 2" screen and Backround Async task is running. Now after some failure event on "Background Async task", I want go back to "Main activity". What can be good way to do this?
Few way I am thinking is, 1) To restart the app completely 2) To create new Intent of Main activity from Background Async task just like we created "Activity 1 & 2".
I am not sure which one from this is good to use. Is there any other way to do it?
I am kind of newbie to android. I am currently using BroadcastReceiver(on Activity 1 & 2)to finish current activity when Background Async task broadcast failure event but sometimes it jump to "Main activity" and sometimes jump to "Activity 1". So not working as I am looking for.
Will you please suggest a good way to do this?
[Sorry, I had to ask this question once again. Because old question was deleted]
#Rohit, I tried way you suggested to finish Activity 1 before going to Activity2. That also works but It create one another problem.
After doing this, when I press back button it directly go to Main activity rather than Activity 1 since it no longer exist.
Is there any solution which can get this both thing working? I want to jump to Activity 1 when back button is pressed. And I want to jump to Main activity when Background Async task created from Activity 2 find problem.
[Edit]
Let me describe more specific case.
Main Activity list open Wifi access point based hardware.
When clicked on particular listed access point, It opens Login activity and ask for user id & password for hardware access. Login activity onCreate function also create TcpAsyncTask background task which will communicate with hardware(which has WiFi accesspoint feature) for authentication and data passing.
When user enter login detail and click on login button, It will create momentary background task LoginDeviceAsyncTask. This task will communicate to hardware via TcpAsyncTask created earlier to verify authentication. If authentication is successful, it will create Navigation activity using startActivityForResult.
In the Navigation activity, It list options for opening specific window e.g. Dashboard. History. When user select Dashboard from options, it creates Dashboard activity using startActivity. When user select History from options, it creates History activity using startActivity.
So if Dashboard activity is opened, it display some data periodically received from hardware using TcpAsyncTask(created earlier in Login activity)
Similarly if History activity is opened, it display some data periodically received from hardware using TcpAsyncTask(created earlier in Login activity)
Dashboard/History activity has options to move to History/Dashboard activity directly. For this it creates selected activity using startActivity and finish current activity.
Now my problem is,
Let's say I am in Dashboard activity and it is displaying data received from hardware using TcpAsyncTask. Now somehow TcpAsyncTask fails to communicate with hardware and at this point I want to move to Main activity directly.
In TcpAsyncTask, I don't have context of activity like Navigation/Dashboard/History which was created after TcpAsyncTask. May be I can get this context in TcpAsyncTask using some MainActivity variables which is updated as per create/finish. Am I correct?
I am current broadcasting to close from TcpAsyncTask and receiving broadcast message in Navigation/Dashboard/History activity using BroadcastReceiver and finish particular activity which received broadcast close message. But sometimes it jump to Main activity and sometimes it jump to Navigation activity when sendBroadcast from TcpAsyncTask to close.
Pass Context in your AsyncTask and through that you can kill your Activity 1 as well as Activity 2
((Activity)context).finish();
You can do this for both activities by passing context of both.
One possible solution is to start Activity 2 with startActivityForResult() then during its processing, you can call setResult() to indicate success or failure. In Activity 1, override onActivityResult() and detect the result of Activity 2. If Activity 2 indicates a failure, simply call finish() on Activity 1 to return to the main activity.
Firstly, remember that code being run in an inactive activity when it is not in the foreground cannot be relied upon. So you should not rely on background async task. Instead, use a Service which does the background processing; this is precisely what Services are meant to encompass (among other persistent processing). While it is quite a lot for a beginner to learn, it will save you a LOT of trouble long term, as its lifecycle is far easier to manage than complex webs of activities and their relationships and lifecycles.
You should read up on android "Back Stacks". If you finish an activity, the previous activity on the back stack will be displayed. These are like the history browsers maintain that determine how browser back-buttons to operate.
So what you need is the main activity spawning a new instance of the sub-activity. Then when the user navigates the back button, the previous activity in the back stack (your main activity) will be resumed. This will not need intents.
However, for an activity to be triggered by a background processing task, I strongly recommend using a Service and intents to co-ordinate it all.

Android - Start a new activity while the application is in the background

The first activity of my application is a splash screen, where some animation is displayed while some loading occurs in a background thread using AsyncTask.
Once the loading in the background is done, I want to start a new activity. What is the correct way to do that ?
Start a new activity directly from the onPostExecute method of the AsyncTask class.
Check if the current activity is displayed before starting the new activity :
If the current activity is display, start the new activity.
If the current activity is NOT display, use a flag (boolean) and check the flag during the onResume method in order to start the new activity there.
My main concern is :
If my application went to the background (due to an incoming phone call, a home key press, ...) and the background thread (AsyncTask) finished executing, and a new activity is started from the onPostExecute method while my application is still in the background : What happens ?
Will the new activity start directly as soon as my application is visible again ?
I faced a similar situation: my application went to background and after some time the app started an intent displaying another activity. However, the app's behavior now depends on the os that it's running:
on pre 4.4 devices the app silently opens the new activity and remains in background. When the user resumes the application, he is prompted with the second activity already.
on 4.4 devices (tested on 4.4.2 and 4.4.4) the app opens the second activity, but after 3-4 seconds, the app pops to foreground, interrumping the user.
Hope it helps anybody. I'm still looking solutions for the second case in order to prevent the app from popping to foreground.
From my experience i am answering your question
Question1
If you using AsyncTask you have to start new activity in OnPostExecute(). In my experience this is the correct way of doing it.
Question2
When ever your press the home key or receiving phone call. Your activity will go in to background until you exit the app by pressing back button(at that time your app is exited). So when you come back to your app your new activity will be visible if background process get finished. Otherwise you will see the start splash screen.
I think that it is dependent upon the OS of android. It has defined some built in priority model for each of the components.
Look at the answer given by commonsware.
change process priority in android
this gives brief idea about components priority.

Android statusbar notification launches new app though it running already. How to synchronize launching app from app icon and staus bar notification?

I have implemented notification within service. Notification works fine. But in the case when app is running already and I clicked on android status bar notification it launches new copy of my app. which is obviously wrong. It should be if user click on status bar notification, should start app if app is not running already otherwise it should opens other activity (message activity in my case). I tried many suggestions provided in similar type of problem posted here but I didn't get solution in my case.
How about setting the launchMode to SingleInstance in the app's manifest.
There's also the trick I picked up on here about having a special NotificatonActivity that the notification calls:
Notification to restore a task rather than a specific activity?
Here i manage to escape out from this problem. First of all i defined Boolean variable in entry activity or class which extends application. i set attribute to that variable true when application runs and false when application exit. and i check that Boolean variable to which activity to open. in my case this solve the problem. may be it helps to your case too.bt scenario may be different. good luck
Using a boolean is not a good solution for this problem. For more info please look at the life time of the application. Instead you should change your launchMode to singleTask or singleInstance in your target activity and receive your intent from onNewIntent() method of that activity.
But be aware, if you call super.onNewIntent(intent); then a second instance of the activity will be created. So don't call that and do whatever you want with your intent.

Start service from main application icon

My application is basically a service, i.e. has no UI (there isn't any class subclassing Activity). I want to have it displayed in the list of applications and to run the service when it's started from there, with the advantage that the user can create a shortcut to display it in the home screen.
Is this possible?
Found the solution to this:
Create an Activity and in the onCreate(...) method call your service and then call finish().
To prevent the flashing of the Activity opening and closing, use this as the theme of your Activity (in the AndroidManifest.xml):
android:theme="#android:style/Theme.Translucent.NoTitleBar"
Just make one Activity that allows the user to configure your application. Also there surely must be some sort of UI as part of the app. Otherwise what is it actually doing and how does the user control it?
I want to have it displayed in the list of applications and to run the service when it's started from there, with the advantage that the user can create a shortcut to display it in the home screen.
That is not a "list of applications". It is a list of activities that have advertised via <intent-filter> that they are to appear in the launcher. The key word is "activities".
Creating a dummy Activity that starts the service and then finishes itself does the trick, but I'm a little worried about the flashing of the Activity loading and closing.
Then create an activity that adds value to the user.

Categories

Resources