close android app with down botton - android

I have a problem with the back button event and a code to close my app. I explain:
I have an app with two activities. The first activity displays a list of events in a listview. When you click on one of these events a new activity that can confirm attendance at the selected event opens. If this second activity you press the button that confirms attendance returns to the main activity.
My problem is that if once you hold an event from the main activity and confirm second activity giving the button that returns you to the main activity when the main activity to give back button instead of closing the app returns to the second activity.
Therefore I want to record the event of pressing the button back and close the app, I tested with:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
android.os.Process.killProcess(android.os.Process.myPid());
return true;
}
return super.onKeyDown(keyCode, event);
}
and too:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
exit(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
thank you very much for your time and help
ANSWER
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
return true;
}
return super.onKeyDown(keyCode, event);
}

I think you have set up the activities wrong. You should set a result in the second activity, and then call it's finish() method. Then the back button will always work as expected and you won't have to do weird hacks like that.

Related

Disable Back button in Digits

While doing digits programming, i am using following method :
Digits.authenticate(authCallback,R.style.CustomDigitsTheme1);
which directs me to digits authentication screen(without showing my xml design file).
Now, when i press back button, it shows me my xml with digits auth button as below.
which i do not want.I tried conventional ways of disabling back buttons but they did not work. Is there any way i can disable back button on authentication???
You can override when the keyboard disappears using this method:
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK &&
event.getAction() == KeyEvent.ACTION_UP) {
// Do your thing here
return false;
}
return super.dispatchKeyEvent(event);
}
This may helps you.
Try Using these
#Override
public void onBackPressed() {
// your code.
}
and for older then API 5 use this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Handle volume button when Option Menu is visible

From my activity, I correctly handled the opening on OptionMenu with this code :
public boolean onKeyDown(int keyCode, KeyEvent event){
if(KeyEvent.KEYCODE_VOLUME_UP == event.getKeyCode() || KeyEvent.KEYCODE_VOLUME_DOWN == event.getKeyCode()){
openOptionsMenu();
return true;
}else
return super.onKeyDown(keyCode, event);
}
But when the Option Menu is visible, I cannot intercept the KyDown anymore. My intend is to be able to close the Option Menu the same way I open it, with volume button.
Any suggestion ?

Back button stuck in endless loop?

My app is currently stuck in an endless loop with the physical back button. The program is set up to load a splash screen and then transition to a main menu. Once at the main menu the user can switch to another activity of their choice. For example: The new game activity. Once the user is at the new game activity I want them to be able to hit the back button and have it take them to the main menu. Once back at the main menu, if they hit the back button again I would like it to exit the game.
This is what I am using for each activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
}
return super.onKeyDown(keyCode, event);
}
It works properly and takes the user back to the main menu without a problem. But if the user hits the back button again once at the main menu it will take them to the screen they just escaped from. So it just loops back to the previous screen every time.
This is how the main menu is set up:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
If I press the back button as soon as the main menu loads the first time it will work properly and close the game. It only messes up if I've hit the back button previously to get from one screen to the main menu.
Update:
Okay that kind of worked. When I hit the back button at the main screen the music stops and it acts like it's trying to close the app but it flashes back to the main screen again. I have to hit it twice every time.
You should call finish() inside the onKeyDown method of your activities so your current activity is actually removed from the stack.
Something like this should work:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
finish();
}
return super.onKeyDown(keyCode, event);
}
First (from comments) add return true; within the if statements (to indicate that you have handled the event), like so:
// Main menu code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
return true; // Add me!
}
return super.onKeyDown(keyCode, event);
}
Additionally, I believe because you're "starting" the menu activity again, you have to clear the back stack (the order of activities used so far) before opening it. You should be able to achieve this using Intent.FLAG_ACTIVITY_CLEAR_TOP (or maybe Intent.FLAG_ACTIVITY_CLEAR_TASK). Something like this:
// Other activity code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent menuIntent = new Intent(NewGameActivity.this, MenuActivity.class);
menuIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add me!
startActivity(menuIntent);
return true; // Add me, too!
}
return super.onKeyDown(keyCode, event);
}
You haven't called finish() in your other activities so when you call finish() on the menu it just closes itself and the other activity is exposed

android : how could I exit the app?

In my android application I changed the back button functionality so that it goes to the main screen of my game , now that it's on the main screen how should I exit the whole application with back button ?
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
return true;
}
return super.onKeyDown(keyCode, event);
}
If you have a mechanism that you can use to see which screen is showing you could do something like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
if(mainScreenIsShowing == true){
//If the main screen is showing let the back button
//have its default behavior.
return super.onKeyDown(keyCode, event);
}else{
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
return true;
}
}
return super.onKeyDown(keyCode, event);
}
You can also check if the user enters the back button two times.
boolean backPressed = false;
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && !backPressed) {
Assets.getInstance().getClick().play(1);
this.clearScreenStack();
this.setScreen(new MainMenuScreen(this));
backPressed = true;
return true;
}
backPressed = false;
return super.onKeyDown(keyCode, event);
}
This is a debateable subject, but I see nothing wrong or with an application exiting when the back button is pressed. After all, a call to finish() is the default behaviour of the back button. If the activity handling your main screen is at the bottom of your activity stack, then a call to finish() will exit your application.
I propose the following: Let your MainMenuScreen be handled in a separate activity, MainMenuActivity, which is the main activity. finish() other activities when going back to the MainMenuActivity, and handle onKeyDown like this in MainMenuActivity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
this.finish()
}
}

Android Home button click ignoring saved state?

Can I ignore the previous stage of application after tapping Home button in android?
So that I can get the application from initial screen.
Use like this.If now you are in LastActivity and you want to go back to the First Activity and there is more activity in between then if you want to go first from last use
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(autoSearch)
if (keyCode == KeyEvent.KEYCODE_HOME) {
Log.d("click","HOMEButtonclicked");
LastActivity.this.startActivity(new Intent(LastActivity.this, FirstActivity.class));
moveTaskToBack(false);
return true;
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources