I have a TabActivity and want to catch and handle presses of HOME and BACK. Where do I need to catch these events?
In my subclass of TabActivity I implement the following:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
// Code handling
}
return super.onKeyDown(keyCode, event);
}
Didn't work.
So I placed a breakpoint on the switch statement line. But this function never gets called, whether I press volume up/down, menu, home, or back. Where do I need to catch these KeyEvents?
It turns out to be pretty easy. Add the following code to your child tab activity :
#Override
public void onBackPressed() {
this.getParent().onBackPressed();
}
Then in the TabActivity do the real logic:
#Override
public void onBackPressed() {
// Called by children
}
Otherwise, the children will intercept and consume the event without notifying the tab host.
I had the same issue and found overriding dispatchKeyEvent worked.
An example of which can be found here for back button press:
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Each tab's Activity handled the "back" presses.
I have a TabActivity and want to catch
and handle presses of HOME and BACK.
Where do I need to catch these events?
You cannot "handle presses of HOME", ever.
With respect to BACK, you can use onKeyDown() (for Android 1.x) or onBackPressed() (for Android 2.x). However, your TabActivity may be too late. For example, if you have activities as the contents of your tabs, it may be that one of them is catching the BACK press and arranging for normal processing (i.e., closing up of the activity). Since I avoid activities-as-tabs like the plague (except for one book example), I have not experimented with BACK button processing in that scenario.
try this in your oncreate()
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
setDefaultKeyMode(DEFAULT_KEYS_SEARCH_GLOBAL);
Related
I want to run code only when the home button (when the app is sent to background) is pressed. I tried using the lifecycle-method but the problem is that they also get executed when and other dialog/activity is started. I only want to check if the application is sent to background. How can I achieve that?
In onOptionsItemSelected(MenuItem item);, check to see if the item clicked is the home button:
EDIT:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Activity.onUserLeaveHint() fires on your activity when the Activity is going to be backgrounded as a result of user action, such as pressing the home button or switching apps.
Well since you are writing the code you can set some boolean flags when you start explicitly another acitivty, and check that when your activity goes through onPause()...
if they are false, it means someone else is pausing your activity.
Maybe not the most elegant solution but it will work if that is the only problem you have.
You could have a tracking system (basically a counter) to know when one of your activities is resumed and paused, thus allowing you to know if any of your activities is open.
This way you could know if your app is "open". but this would not apply only to the home button, but to the back button (last back to close your app) or even opening another app from the notification bar.
Android OS will kill your application to free resources, it wont stay in the background all the time. Use service if you want your app keep running in the background
If you want to do when the home button is pressed and the activity is going background, you can override this on the activity:
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
switch(keyCode)
{
case KeyEvent.KEYCODE_HOME:
//somthing you want to do
default:
return super.onKeyUp(keyCode, event);
}
}
I am using the KeyUp event because that's when the app goes backgroud, the user can do a longPress and I don't think you want to do the method when the app still open.
this is sort of a cheat answer, but i find it to be much more reliable.
one method that's always called when you press the home button is the surfacedestroyed method for any surfaceviews contained in any of the program's activities.
Putting the code you want to execute there, having the method point to another method you want to execute, or having the method trigger a flag that points to the method you want to execute will do the job.
I'd like to destroy my application when the user touches the home button of Android device and begin the MainActivity when the user touches the "back" button of Android.
Does any ones knows how to do that?
For close app on Back
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK :
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
And You can't get click event of Home Button so you want to code onStop method.
#Override
protected void onStop() {
finish();
super.onStop();
}
System.exit(0)
But it's best not to use it. Android isn't designed for this purpose.
Close application and launch home screen on Android
You can do this by calling the finish() and finishActivity() methods. checkout the details on API guide Shutting down an Activity. From where to call these methods is based on how your application is implemented, but I guess you can do this from the current focused activity by listening to KeyEvent and filtering on Home button key event.
However you need to consider that once you have killed your activities pressing the back button will not get you back to your application activity since killing the activities will wipe them out of memory stack.
Also check out the Activity life cycle diagram and detailed description given on Android Developers site.
You can close an Activity by calling finish(), but you'll have to do that for each Activity that is open. To have this happen upon pushing the HOME button, you'll have to register a KeyEvent. I'm not too clear on how to do this, but you can find documentation here.
call finish() in your onStop() method. Or use android:noHistory="true" in your manifest.
I have open pdf file through my application.when click on device back button it is automatically come back to my application .It is working fine.Here i want catch back button event in device.I override the back button.but it is not working.please help me.
This is an example of what you are asking:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
//do your stuff
}
return super.onKeyDown(keyCode, event);
}
just call the foolowing function this will close current activity and move you to the previous screen
finish();
You would override the onKeyDown event in your Activity class and look for the keyCode of KEYCODE_BACK. To prevent further propagation you would return true to stop the system from handling it and this should stop the back button from taking your user out of wherever you are.
This violates the rules of what a user expects to happen, though, and is not recommended unless you're overriding the back button for something like going back through pages in a WebView or something like that.
public void onBackPressed() {
Intent intent = new Intent(CurrentActivity.this,
PreviousActivity.class);
startActivity(intent);
return;
}
If you have handled pdf file related operations(like reading etc) in your application i.e. there is Activity or Fragment handling this pdf file operations in your application then you can overrided onBackPressed() method to put your logic there. But if you are opening pdf via intent i.e using other application using intents action parameter then you can not overrided there onBackPressed() or onKeyDown() event in your application. But instead of that you can put your logic inside overriding onActivityResult() method in your activity from which you are opening the pdf file.
I'm developing an application in Android, and I use the the following code to handle the KeyEvent for the back button:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
How would I go about doing this for the home button?
You can not control the behaviour of Home key. You will not get the event of Home key but you can disable it but it is highly recommended you should not do this.Before blocking the Home key refer this post.
However you can block the home key like this:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
As this question suggests: Android Overriding home key this is unfortunately not possible. Perhaps there is some other way to implement your desired behavior?
Obvious answer is - handle home key press in onPause() method of your activity. This callback is called when user hits home key. Guaranted. Home key is not for you, but
for OS and user.
Read these :
Overriding the Home button - how do I get rid of the choice?
Android - How to catch that the Home button was pressed?
The home button is supposed to do one thing and one thing only and consistently. Get the user back to the the HOME screen. Even if you could override it's behavior it would be an extremely user-unfriendly thing to do. So don't do it and solve your problem differently!strong text
I have almost finished developing an Android App. I use the GPS location and sms receiver class. I can see that if I press "HOME"(the house), it's still running. I would like to close the GPS listener and the SMS_RECEIVER when home button is pressed.
I am using eclipse and windows.
You can listen the onStop Events and shutdown the listeners.
Activity.onStop documentation
Note: maybe onPause or onDestroy might be better options. Read activity life-cycle and choose the best point to do this.
Using the home button to exit will leave your app running (home, from what I understand, is more of a "minimize" button). Use back to close completely. To override the home button functionality to actually exit, use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_HOME:
finish();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
If you use BroadCast receivers(you probably use separate classes and declare receivers in AndroidManifest.xml) then consider making them class members of your Activity. Inside Activity class override onResume and onPause and register and unregister receivers there.