An issue has appeared a few days ago.
I have an application that listen for GPS location. The listener is a background service that works all the time, this service saves data in application level and each activity reads this data. So, when i press back button i am able to catch this event and i can stop the service, but when i press HOME button the service is still working although the application is in background mode and this consumes battery, because the GPS always works. How can i handle this event? I do want to stop all services when the user presses HOME button and start them again when user gets back.
10x
It sounds like you're catching the back button either via an onKey... method or in onStop. You should place your code in the onPause() method to ensure it's used whenever the app gets backgrounded.
You can not handle home button events in your Android application. Google has made it for internal use only.
LINK 1
LINK 2
#Override
protected void onUserLeaveHint()
{
// When user presses home page
Log.v(TAG, "Home Button Pressed");
super.onUserLeaveHint();
}
You could create a OnKeyListener, and check if the pressed key was Home. I never tried it, though.
create a custom activity called customActivity extends Activity now override method(in customActivity) to catch home button event and stop your service(create & start service in application class). Now extends customActivity instead of Activity for any activity_class.
Long press the HOME button, it will enlist the running process, select the one you want and then exit it gracefully.
OR
From Home -> settings -> application -> manage application
you can kill the unwanted process.
Related
i have two simple questions (hope so).
First i want to close my app, when the user hits the homebutton in my HomeFragment, because there is a counter (Days,Hours) and when I click the home Button and come back later there is still the old value.
Then i have to close the app via backbutton and start again. So i want my app to get killed, when I hit the home button.
Then i have a button to delete the last value, on which the counter is based. When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
No, these functions are meant to be called by the system.
You are probably looking for the onStop() method (also called by the system)
protected void onStop()
{
super.onStop();
// clear counter e.g.
myCounter.setText("");
}
Other than this, it's kind of hard to tell what you want. Feel free to add more (if not all) of your code.
I have an app for Galaxy Note in which i have to check that "home/Center" key is pressed. Is there any function for it like for when back button there is a default function onBackPressed. Any help would be appreciated.
There is no way to catch the home-key in android. Ideally it is not for developers.
But you can do the following
onPause will be called if following events occur
a. User Press Home Key
b. User press back Key
c. External interrupt like calls
So since you can handle backpress, and listen on incoming call broadcast, then you can determine in the onPause method if it is due to home press.
Parse the logcat and then do necessary action in the background service
I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}
When you press the home button, what does it do by default?
I want to keep what it does by default, but make sure it ends my music as well.
For example:
public void onBackPressed() {
return;
I disabled my back button.
I want to make it so the home button does what it does, but i want to call my
this.stopService(new Intent(this, Music.class));
in the method too.
It is not possible to override "Home" key press just like you did for Back button press. In fact you're not supposed to over ride home key press because user presses Home key to quickly come to the launcher.
Even if you try to override onKeyDown(), you'll see that Home key press will not invoke this function.
But pressing Home key will surely call onStop() of your activity and hence you can stop your music service in onStop(). Hope this helps.
When you press the home button, what does it do by default?
It brings the home screen activity to the foreground.
I want to keep what it does by default, but make sure it ends my music as well.
Stop the music in onPause() or onStop() of your foreground activity, as these will be called when it loses the foreground to the home screen activity.
If you are talking in coding sense -
The home button brings the default launcher to the foreground.
Call onPause() or onStop with super.onPause() or super.onStop() inside of the method.
If you are talking in literal terms with nothing to do with code then..
The home button launches the default launcher activity, this can be cleared if you installed the launcher on some devices by going settings > applications > manage find the application and then go clear defaults
When the music stops is decided by the developers code, you cant change how it works sorry.
I am developing an Android application. I want to quit my application but application should be in running state in background. Whenever I click on application application should start from last quit point. I dont want to login again.
The same thing is happening when I press home button. But I want to implement similar functionality like Home button on my own button event. How should I proceed with that??
Though I have finished all other activities, still I need to login again. When I finish the activity my session ends there. And on next app start with login screen.
Whereas in case of home button click, it keeps my session and on next app start my app check onResume() event where I am checking whether session exist or not. If session is there I can enter directly into my account.
So Anybody have any idea what exactly android does when we press home button.
The best way I know is something like in your scenario where you want to quit:
Save the login credentials on login and clear on log out.
When your app launches check for credentials and if it is there then you should login without asking user.
The above process will not bring your last activity but the activity which you show after login.
To bring back the last activity where you quit, you can use moveTasktoback(true) as it will put your app in the background the same when you press Home key but it's behaviour will not remain constant as android can kill your app whenever it will require the memory.
You mean login to a web app? WebView saves cookie information application-wide regardless of activity.
Otherwise use a static variable or singleton class to hold any session state and on activity start, check the static variable or singleton class for any state else redirect them to the login screen.
You can call moveTaskToBack on your Activity.
create a button where you want to pause(i mean in which activity) the application. And write the code like below. This will pause your application.
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
moveTaskToBack(true);
}
});