Application Back button android [duplicate] - java

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

Related

Is possible to show a dialog before show Recents, Press Back or Home Button?

I'm trying to detect when the user click on BackPressed (this I can override the onBackPressed()) also the home button and the recent button.
My goal is, don't quit the app until the user Accepts a dialog, I mean, for instance I'm using an app, that doesn't let go to another app to see something, so I want to do something like a "Security thingy".
Scenario would be :
User is watching a video (that's an example)
User tries to press the square button to show recent apps
Then user sees a dialog saying if you leave you are gonna loose the video (whatever...) if he press yes do the normal stuff like see the recents if he presses no, don't show recents...
The same with the home button
I've already tried onPause() but the thing is that I see the dialog after the action is done and if I do an if condition it crashes because it needs the super.onPause().
Any clue how to achieve this?
I think you only can handle the back button. For back button, you can override the onBackPressed() method of your Activity and show the user a dialog and then call android.os.Process.killProcess(android.os.Process.myPid()) when the user clicks on Yes button of your dialog
#Override
public void onBackPressed() {
//show dialog
}
private void onDialogYesButtonClick() {
android.os.Process.killProcess(android.os.Process.myPid())
}

How can I detect when a user exit my app?

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

On Close button action Java [duplicate]

This question already has answers here:
How to capture a JFrame's close button click event?
(6 answers)
Closed 9 years ago.
Sorry if there is a question like this already but I couldn't find it.
I found things like how to create a button listener and people create a button and perform action on it. I want when the close button ( x button ) is pressed a warning window to pop-up and say that the project is not saved. I couldn't find how to access the close button. How to use a button listener with the close button? Hope made myself clear.
Thanks
You need to add a WindowAdapter to your JFrame.
myFrame.addWindowListener(new WindowAdapter(){
#Override
public void windowClosing(WindowEvent e){
// do something
}
});
Now, every time someone presses the close button, the windowClosing() method will be called. Check if the user has saved the work. If not, either auto-save it like or promprt user to save it.
overload the close/exit action
http://docs.oracle.com/javase/tutorial/uiswing/components/frame.html
Create custom operation for setDefaultCloseOperation?
define a method separately and add listener for close action.

Android Back Button reacts too quickly

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 :)

Block the application in Android

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.

Categories