When creating one Intent so that MyActivity reacts to a User pressing the Home-button is easy using the XML markup:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
I want to know how to avoid getting the choice of "what activity do you want to use" for the Home screen? HTC has made its "Touch Flo" (Sense) override the default "start" Activity and I never get the question if I want to use "Start" or "TouchFlo" usually. However, when I added my own Activity I always get the question.
Yes, I know that I can check the "Use this as standard"-checkbox, but that's not what I want right now. So, question is: can I make the system override everything else and always use MyActivity as default?
Next, I really only want to override the normal Home Screen when my app is running. If its not running, everything should work as normal, ie MyActivity should NOT be associated with the Home button.
You can't permanently override the Home button without the user confirming it.
One argument for why this is the case is a security one. The Home button is the one way a user can be guaranteed to exit any application. If you could make the Home button launch your application instead of the Home screen without the user confirming this change, it would then be very easy to write a malicious application that hijacked a user's phone.
Alternatively, your application could contain a replicate Home Screen that harvested a user's Google account details; not that hard since the source is available. If your application could silently replace the default Home Screen, it would be hard for the user to tell this had happened.
Also, do you really want to override Home? Home is like an emergency escape button. Pressing Home twice will always take a user back to the center panel of the Home Screen, so whatever application they're running, it's easy for a user to get back to somewhere they know. You shouldn't really be overriding this unless you're producing a full Home replacement.
#Override
public void onAttachedToWindow()
{
Log.i("TESTE", "onAttachedToWindow");
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
With this method, the HOME Button stops working in this activity (only this activity). Then you just reimplement as it was a normal button event (the back button for instance).
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.i("TESTE", "BOTAO HOME");
return true;
}
return super.onKeyDown(keyCode, event);
}
The overridable onUserLeaveHint() lifecycle hook can be used when the user presses the home button.
This hook has been present since API level 3.
#Override
protected void onUserLeaveHint() {
super.onUserLeaveHint();
// Your code here
}
More info about the method:
Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice.
For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted.
In cases when it is invoked, this method is called right before the activity's onPause() callback.
Source: https://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
Well, the user would still have to choose the regular home as their home with use as default checked to stop the prompt from coming back. However, I believe it is possible for you to then modify the system settings in some way to incidentally have your particular activity be considered the default home, such that press of home would then do nothing or appear locked to the user at the main activity, and I understand you wish to launch your user into other activities from there, giving them a fast home press return to this root Launcher. I can see the benefit of this completely, and it may even benefit what I'm developing if I choose to implement multiple widget screens that the user can flip left or right between.
There is at least 1 app out there that I've downloaded which appears to be doing exactly this. When the user exits the app, it restores the user's regular default home preference. The app instructs the users very explicitly to choose Launcher as their default home and never do that with the app itself. It has a few different "exit" methods which give the user the choice of which home to return to if they have multiple, and one that just exits to their regular default.
I am researching this as well and will report back with progress & source.
Use onUserLeaveHint().
onUserLeaveHint() is a protected method as other lifecycle methods of the activity and if you are handling onUserLeaveHint this will take care of the following case
When User hits home key
When User hits back key
When User hits annunciator bar
Here you can catch Homeb button click.
Related
I'm working on an Android app that will show college fitness professors how their students are doing in their classes. Since this data is fairly sensitive (biometrics are shown, including weight, something many college students are self-conscious about) I don't want the data to be available to anyone who picks up the tablet. While I have a proper login screen created, complete with authentication for the database, etc. I have an issue when the home button is pressed. Since Android doesn't close a program immediately on leaving the app, it's possible to reopen it and return to where you were. I would like to force the app to return to the login screen each time (I've altered onBackPressed so you can't just return to the previous view from the login screen) so that you have to re-enter your credentials to get back into the app. However, I can't seem to do this. An answer I found on here said to use the following line:
android:clearTaskOnLaunch="true"
However, no matter what XML file I put it in, be it the Manifest or the individual Activity XMLs, it appears to do nothing. So, how do I ensure the login screen comes up each time the app is launched, regardless of whether it is starting from scratch or not?
Try to play around with onUserLeaveHint() method. If you read its documentation, it says:
Its Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called
So, when ever you detected home button pressed, you can finish the running activity/activities. So next time user click the app, it will start from the first login screen.
Hope this helps.
You should override onUserLeaveHint()
#Override
protected void onUserLeaveHint() {
// do your logic
//finish() or start login activity
}
You could set a flag when onPause() is initiated within the activity. And then when you return you could check the flag from within onResume() and then request a login from that point. This will be sure to request it each time; in a simple case of course.
Edit:
With multiple activities, you could check against a saved context to see if they are the same when you start a new activity. If the contexts differ then you can discard the context previous activities context and start a new activity.
If they are the same, then you have come back to the activity from itself (you have lowered and brought the screen back). You would have to use some form of saved state such as that to do it in this manner with multiple activities when outside the case of a simple application.
I found out how to do it in my case. For any others with the same problem, try following the example here:
Android detecting if an application entered the background
I know that android does not allow us to catch the home button press; however, I have my own built home screen replacement app and want to just know when the button was pressed to allow animations.
So, my app has just one activity as I do most of my "activities" as canvas drawing so I can have complete control over the visual aesthetics of my app. The problem is if the user navigates to a different page within the app and presses home, nothing happens, since the app is technically already in the same activity.
I want to know when the button is pressed so I can then animate/navigate my app back to the main screen. Also, I know I could accomplish this by having separate real activities however that won't work for what I'm trying to do.
You need to add <category android:name="android.intent.category.HOME"/> to your activity's intent filter in your app's Android.xml
You can take a look at "Home" app in Android SDK samples. They can be downloaded using these instructions.
//overriding method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
}
});
You can detect when the home button is pressed because onPause() is always called. You are not allowed to override the home button, but the following should work
#Override
protected void onPause()
{
super.onPause();
//code here
}
Of course the problem with using this is that it will be called even if the user is not quitting the app(ie. If they are sent to another application through an intent)
That is the closest that you are going to get to a onHomeButtonPressedListener there are really no ways to do this
Home key press Event Listener
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.
Im connected to a chat server which gives me messages i print out in a textview. This continues when the user leaves the application by pressing the home key etc etc. I would like to close all streams if the user goes back with the phones back button to the previous activity. Problem is that onStop() and onPause() are both called independent of if the Home key was pressed or the back key. Its just called when the activity loses focus or visibility, doesnt matter which way it happened.
How do i find out if the back key was pressed, and not home?
You can implement your own version of onBackPressed(). That way you'll know every time it's pressed and can do what ever you need to in that callback. So do something like this:
public void onBackPressed(){
//Do the stuff you want to do
//Then call the parent class version function to allow it to do any other stuff
// that needs to happen as well.
super.onBackPressed();
}
I would like to display some images when the application is opened for the first time. Or if its being reopened. I don't want the application to be killed when the user presses the back button, to go to the home screen. But instead keep it alive but still return to the home screen.
As Andro_Selva said, the back button finishes your activity; it doesn't kill the app.
If you want to accomplish something similar to pressing the home button (so your app is hidden, but the activity is still alive), you can do something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
It's a little different than pressing the home key, because it will only take you to last app you were in when you launched this app. So it won't necessarily take you back to the launcher. But this behavior will be closer to what the user expects when pressing back.
You may also want to put this in the Manifest under your root Activity :
android:alwaysRetainTaskState="true"
This will prevent Android's default behavior of finishing all your Activities if they have been idle for about half an hour.
add this to your activity
public void onBackPressed() {
//finish(); this is what would normally happen
}
This way you have the full control on the back key. As for the home key, you better not do anything to it. otherwise it could be a big problem if something goes wrong with your app, and the user wont be able to get out of it. Also if you look at how android works, when a home key is pressed, the app isn't killed, just paused, unless the system decides there is not enough memory to keep your app, in which case it will shut down.
how to override backpress to not kill activity?
I believe that you have not understood certain things here. When you press your back button it doesn't mean that you are killing your app. It means that you are finishing your activity and which also means that your app is running in the background. If you want to kill your activity you have to do one of these,
by calling System.exit(0);
Or by
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
Or by manually getting into settings->manage applications->select your app->click on force stop
When you press your home key it means that your activity is paused or something like freezed and when you get to your app again, you can see your app resumed from where you left your app.