Adding use to the home button - android

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.

Related

Why does "Home" button call `onDestroy()` in my Android app?

On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.

How to complete the application when pressed buttons Home i Back?

How to complete the application when pressed buttons Home and Back? Provided that in my memory holds many pictures.
When pressed on the Back button - application restarts... When pressed on the Home button - quit application, but when it restart - does not start Splashstsreen.
Hard to see without code but it sounds like your activity is resuming rather then starting from scratch (as it should behave). It sounds like it's behaving correctly in that case. If you insist in wanting your application to truly quit after the back button is clicked perhaps you can override onBackPressed() then have your Activity call its finish() method.
I don't think it's good programming practice to fiddle and interfering with the activities lifecycle. It's the OS responsibility to manage the lifecycle, including pause and finish activities.
Instead you should use other methods to handle your problem with not showing splash screen, these methods are onResume and maybe also onStart(). Also you should get familiar with the activity lifecycle(link submitted by #ss1271).
Press Home will let the activity to enter onPause(). however, if you insist to quit the application when press HOME, which is obviously not a good practise, you can do like this:
Override onPause() and onBackPressed()
add finish(); to these methods.
I am sure by adding finish(); to onBackPressed() will quit the app when Back button pressed, but I haven't tried on Home.
Please refer to Activity Lifecycle for more info.

Quit Application when home button pressed.

I have a security program, so i need to quit the application when the HOME button pressed. But I know the HOME button action cannot be implemented.
Then, I know that I can use
android:noHistory="true"
to do it. But I have used
startActivityForResult(intent, 0);
to call a new activity. When the activity called back by
this.setResult(RESULT_OK, intent);
this.finish();
The original activity has already quit.
How can I quit the application when the HOME button pressed, but the startActivityForResult will not quit the activity?
Don't quit the application - that's not how android works.
Instead, in a desired situation, perhaps onPause(), destroy the security credentials that let your app function. On the next run or resume, the user will have to re-authenticate.
Perhaps you can use onUserLeaveHint() as an indication that you should finish(). This is called when the user intentionally leaves your activity (ie: by pressing HOME key)
As a general rule, you shouldn't be quitting your app like this. It breaks the intuitive workflow most people expect when using an app. However, if you really want to do this, override onPause() and onStop() and implement the code to quit in those. Remember to call super() in your overridden method.

Difference between android Home key and Back key and their behaviour

Can anyone point me out or explain what is the difference between android Home key and Back key and their respective behavior related to an android app/activity.
Thank you.
Back Key :
If you press Back Key, onPause(), onStop() and onDestroy() callbacks will called.
Activity will created again by system calls onCreate() callback, then onStart() and onResume() callbacks will be followed.
Home Key :
If you press Home Key, onPause() and onStop() callbacks will called.
Here Activity will restart by system calls onRestart() callback, then onStart() and onResume() callbacks will be followed.
In addition to #Fosco's comments, using back will usually cause an app to exit, where home will leave it running. This is dependent on the application, but the general pattern is to exit the app when using back on the last activity.
Back key destroys the current Activity, home key doesn't. In the Activity lyfecycle, pressing back calls all the way to current activity's onDestroy() method. On the other hand, pressing home pauses the Activity, which stays alive in background.
The home key takes you to the home screen, the back key takes you back to the previous activity (or home if there's no activity to go back to.)
If you are at the home screen and launch Messaging, then hit back, it's the same as hitting the home key.
If you're in Email and get an alert for a text message, and you choose the notification which takes you to Messaging, then hit Back, you'll go back to Email.
edit: as mentioned by Tim Coker, when the back button takes you to the home screen, it usually terminates the activity. I think this is based on the app, whether it terminates or stays resident.

How to handle that the application is minimized by HOME button

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.

Categories

Resources