I have a service that runs to get user current location through LocationListener. I stop the service on the activity's onDestroy() but this method is only called when the user hits the back button to exit the app.
I want the service to stop both when the user hits the back button to exit and when they hit the home button. However, as far as i know, there's no way to intercept the home button. How do people deal with this issue?
Is it normal to leave the location listener running after the user hits the home button?
Use onPause() instead on onDestroy()
Related
I have developed an android watch app. It has a play button which when you click starts the sensor manager to analyse the sensor data. Using the data, I recognize the gestures performed by the user. So when I press the play button, sensor data analysis starts and the user can see a stop button to stop the sensor whenever he wants. So once the sensor is started, if I swipe and go back to the home screen and the again open the app, I can see the play button instead of stop button. I want to see the stop button when the user returns. Ideally I want to run a foreground service to detect the sensor readings.
So I want to start a foreground service when the user hits the play button and when the user open the notification, he must see the stop button to stop the sensor data as well as the service. How can I do this? Please advise.
UI:
Save your required UI data to the Bundle of the current states (stop button appearing) using onSaveInstanceState for that instance to your onPause() so that it can be retrieved by the onResume() using onRestoreInstanceState and viewed.
Service:
The Service should be bound to the activity so the two communicate directly. The service can be stopped from the activity using
stopService(new Intent(this, MyService.class));
You can also call stopSelf() from within the Service.
I am starting a service in my app. On click of service I am launching an activity. The service click event works fine when we go to any app and press back button and exit the app.
But if we are in any app and then press home button and click on the service the activity is not launched. If I click it more then 2 times,it opens the activity and sometimes it opens the app also.
So i am unable to understand the difference between pressing back button and home button.
After you start an activity, if HOME key is pressed, then the current activity is stopped and its task goes into the background. The system retains the state of the activity - i.e. onSaveInstanceState will be called. If the user later resumes the task by selecting the launcher icon that began the task again, the task comes to the foreground and resumes the activity at the top of the stack.
However, if BACK key is pressed, the current activity is popped from the stack and destroyed. The assmuption is the activity is done and will not be used again. So the system does not retain the activity's state - i.e. onSaveInstanceState will not be called.
Home Task :
Pressing the Home switches you from the app to the home screen, whilst leaving your app running in the background. This is a bit like switching between windows on a Windows PC.
Except that when your phone is running low on resources like memory it will start to close apps that are running in the background, so that your phone has enough resources for what you're trying to do now. Games are often amongst the first apps the phone will "kill" to save resources as they often use a lot more memory and CPU than other apps. This is why sometimes your game is still running paused, and sometimes Android has closed it for you.
The Back button is the way to close apps so that they are actually closed.
onPause() is called in Activity A when it launches Activity B. After the back button is called in Activity B, onResume() is called in Activity A.
In case of activities their default implementation is LIFO based in stack and works like:
On Back button Pressed: finish the current activity by calling stop method.
On Home button pressed: activity is being paused and then it may either resume if come back to it, otherwise system will call stop() method of activity to save unused resources and utilize memory.
but these functions can be edited by overriding if required.
I want to run a SERVICE when my app is no longer visible for example if the user pressed home button or killed my app.
how can I implement that?
You can run that service in onStop of a Activity.
Onstop is Called when the activity is no longer visible to the user.
It won't work if the application crashes.
You can run that service in onPause of a Activity. OnPause is Called when the user press home button. For Back Button, OnStop is Called. It won't work if the application crashes.
You can override onPause method, which runs when user presses home button and your application go in background, and onDestroy which called back when your application is been killed.
Check this
I am writing a GPS based application for Android. I want a button to exit the application which will turn GPS off but I do not want back arrorw or home to turn the processes off.
I have read This thread and many others that do a great job of explaining why I don't want to do what I want to do. The issue is that there are background processes and GPS is left running if I just back arrow out of the application. I understand that I can turn these off with OnStop() or OnPause() but I don't want back arrow or home to stop anything. I want all the GPS and handlers to continue running no matter what the user does unless he hits EXIT on my main screen which takes him to another screen that is to confirm the exit and do the cleanup. That way the user can back arrow out of the exit screen without actually exiting if it was hit by mistake.
Please understand that this application will be used in bright sunlight in adverse conditions with gloves on. It is very difficult to read anything so I use high contrast fonts 100 high. It cannot stop unless the user really wants it to stop. But just letting it run will kill the battery. I have no problem with the behavior of the system if I just let it run, just with the idea that the battery will run down and I don't want any simple button push, which may be accidental, to stop the services. I want it just as hard to kill this apps background processes as it is to turn the phone off, but I want to be able to do it.
I have tried finish(), which is just a back arrow, and every other method I have seen over the last three hours of Googling. I have thought of changing my EXIT button to a Turn GPS off button but then if the user uses back arrow to exit he gets to the main page and that turns GPS back on.
Just to be clear. My app has a menu based main screen. Any useful page will return to that menu if back arrow is hit. One of the menu items is EXIT. Hitting that menu item takes you to an exit screen that has a button that says EXIT. If the user got here by accident, he can back arrow back to the menu. If he hits the EXIT button on the exit page, I can turn off all the handlers and GPS and go to the phone or tablet home screen. At least that is what I want and can't figure out how to do.
I have used similar apps that use OnStop() or OnPause() to turn off the GPS and they are a pain. That is not what I want. Please don't tell me that is what I want to do. I know how to do that. That is not what I am asking.
Thanks.
UPDATE:
Thanks to Chris I have ALMOST solved this. I can turn my handlers and GPS off with my EXIT button and then run this code
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
UPDATE #2
The problem that I just found in further testing is that the application goes back to the EXIT screen when it is called for a second time.
UPDATE#3
Adding finish(); after startActivity(startMain) seems to work. The finish does a back arrow to the main menu. Hopefully once I get all my code installed it will still work. I removed the third line above so what I now have is this:
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startActivity(startMain);
finish();
There is a really simple way to resolve this. Your main problem is that you need the EXIT activity to not go back to the MAIN activity (ie: After the user EXITS, the activity stack should be empty) if the user wants to exit.
Have the MAIN activity call the EXIT activity using startActivityForResult(). The EXIT activity can then return RESULT_OK if the user really wants to exit, and it will returnRESULT_CANCELED if the user used the BACK button to go back.
The MAIN activity then gets the result in its onActivityResult() and can call finish() if the user really wanted to exit, or just do nothing if the user cancelled the exit.
This will result in the activity stack being completely cleared if the user wanted to exit, so that the next time the user starts the app, it will go to the MAIN activity.
I have thought of changing my EXIT button to a Turn GPS off button but then if the user uses back arrow to exit he gets to the main page and that turns GPS back on.
That is the root of a more proper solution than trying to "exit" the process - you just need to handle the accidental re-activation.
you could automatically send a HOME intent after disabling the GPS.
you could override the back button there, and send a HOME intent instead (if the GPS is off).
you could set a flag in shared preferences when you turn off the GPS to indicate that it is supposed to be off, then check that in any Activity where you would otherwise turn it on automatically. If you detect that the GPS is supposed to be off, instead of your usually choices just show a dialog that the GPS is off and ask if they want to turn it back on, or "quit" (ie Intent to the home screen).
i am working on my App. in between i pressed the Home button on the device. my app will go to the background tasks. After that i long press the home button it will show my app as a persistent state. i.e where and what i had done in my app. But i click my app in the directory window it restarts my app. i want to do if my app is in the background tasks it will wake up else it will start. how to achieve that? Any Idea?
When you do that Android will call:
onRestart()
onStart()
onResume()
You can override that methods in order to do what you need. I wouldn't recommend it, though.
Remember that if the device needs memory, it will kill your process and when you open your app again Android will call the onCreate() method of your Activity.
Check the Activity's lifecycle.