I'm developing an android application and when I press the back button from my device (normal press time for a person, 1 second or less), it skips from my activity, to the previous activity (menu) and then exits the application.
But if I tap the back button quickly, it reacts as expected, it goes to the menu.
I've tried to find a solution but no success.
I've always tried to override the back button default behavior but no success either.
Is there a way to set a reaction time for the back button to react?
Many thanks in advance!
P.S.- I have other activities that maintains the expected behavior in back button when pressed with a normal press time.
"Is there a way to set a reaction time for the back button to react?"
Yes, you can simply record the time when the button is pressed and react differently in onBackPressed by calculating the (currentTime-lastTimePressed)
To allow this to work with previous activities, you can ask activities to startActivityForResult, so that when you finish your activity you can pass on the time as well to let them know if they should exit as well.
I was developing an extra option for an application that already exists, and I found out that I should extend not from the Activity from Android but an already extended Activity called SEActivity. So in this extended version of Activity they override the method onKeyDown like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
else
return super.onKeyDown(keyCode, event);
}
By extending this SEActivity, the Back button works with the expected behavior.
Thanks anyway :)
Related
I'm working on an Android project in Java and I'd like to get the MotionEvent when the user hits the back button (onBackPressed).
I know how to override the back button press, and i know how to get the touch event from touches in my activity, but I couldn't find a way to get the touch event details out of the back button press.
The goal is to write the user's touch event details (as if the user touches a regular view) to a file, even when the back button is pressed.
Is it possible? if so, how can I do it?
I'm writing an android app and I want to remove the user from an array when he exit the app. I tried using the onStop() method, but it's not woking as I desire, because whenever an activity changes, the user is removed from the array.
I need a way to execute a method when the user has removed the app from "recent activities".
Any suggestions on the matter?
You can track the back key so when the user presses it to exit the app you do what you want.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
//YOUR CODE HERE
finish(); //<<<------MUST BE USED SO THAT THE BUTTON WILL ACTUALLY FINISH THE APP
}
return super.onKeyDown(keyCode, event);
}
Otherwise you can try the onPause() method instead of the onStop. Android doesnt recommend using the onStop to save data etc. I think though that the back key is best
I need a way to execute a method when the user has removed the app
from "recent activities"
When you swipe an app from the list, it actually just stops all the app's services (and not the app itself!). You may want to consider creating a service just for this purpose so that when the service is killed, you are notified via onTaskRemoved.
You can read some more here
This question already has answers here:
How to detect "Recent Apps" system button clicks (Honeycomb+)
(8 answers)
Closed 9 years ago.
How can I detect when I press the return application button in android? it is on the right side of the home button on samsung s3. sorry I dont really know what is the name of the button.
Not the back button of the keyboard
It is called the Back Button. You can override its method to handle click events. Something like
#Override
public void onBackPressed()
{
// do stuff
super.onBackPressed();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.i("MainActivity", "Back button pressed, exiting..");
//Your code here..
}
return super.onKeyDown(keyCode, event);
}
Like this. i checked if it is back button. you can look up for the right button constant inside 'KeyEvent' class.
I guess you mean the button for opening the Recent Apps (the one on the right in the image):
There's no way to overriding it. The only method you can intercept is the Activity's onWindowFocusChanged, which is called once the recent apps is displayed, but also is been called on a lot of other different situations.
Take a look here for more info:
How to detect "Recent Apps" system button clicks (Honeycomb+)
And here if you want to avoid opening the recent apps (but it seems it doesn't work on Jellybean and up):
https://stackoverflow.com/a/17095749/1991053
In one of my activities, I want my UI to be dimmed at all times. When the used presses a navigation button (back, home, recent apps) it should act like normal, but when focus is back to my activity, it should go back to lights-out. Currently, I have lights-out working, but after I press Recent Apps, the lights stay on forever.
How do I make my app always stay in lights-out?
Figured out something that worked.
#Override
public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus) {
lightsOut();
}
}
}
Basically, every time my app gets focus, it forces lights out (possibly redundantly, but it doesn't really matter).
Works like a charm!
For my application I have implemented a system that detects if the current version is an old version that requires an update. By the way the detection work as intended.
My problem is how to block the application use when that happens. By now I'm launching an activity for inform the user and grant a link to the Market. My problem is that if the user press the back button returns to the last Activity and I dont know how to change the
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
} else {
return super.onKeyDown(keyCode, event);
}
}
For exit the application when the user press backs and don't go back to the other activities.
Thanks!
You can call finish() on the other activities, but I'm not sure how you'd get a reference to them.
To my knowledge the Android system does not let the programmer end tasks (which contain multiple activities in their back stacks), which is what you're trying to do.
You could try using Fragments instead of Activities, which will let you communicate information and events between them.
Before laucnhing the new activity, make sure to call finish() on main activity (outdated version of main app) to remove from back stack.