android : how could I exit the app? - android

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()
}
}

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);
}

close android app with down botton

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.

NavigationDrawer & KeyEvent propagation

When in my Activity i override onKeyDown function to control keyback action on opened NavigationDrawer like this snippet
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
if (mLeftDrawer.isShown()) {
mDrawerLayout.closeDrawer(mLeftDrawer);
return false;
}
}
return super.onKeyDown(keyCode, event);
}
returning false instead of true, where is received the resulting propagation (to up) of the event?
Thanks
Hi GPack I do the same in my app, control keycode_back for control a little bit navigation drawer but I use a different code, like this:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
//Control Navigation
//Floors level
if(keyCode == KeyEvent.KEYCODE_BACK && ActualFragment == 0)
{
if(drawerMenu.isDrawerOpen(Gravity.LEFT)){
drawerMenu.closeDrawers();
}
else{
RemoveAndReplaceFragment_ToFloors(getFragmentManager());
this.moveTaskToBack(true);
}
return true;
}
return false;
}
I think this it's that you needs... Advice me if you need more help or same!!
Good luck GPack!

How to keep DropDownList of AutoCompleteTextView opened after pressing the Back key?

i am using AutoCompleteTextView in my Activity and i need it's DropDownList to be shown all the time (it's the only View in Window), even after Back key press. I need to dismiss soft keyboard instead.
I tried to override Activity's onBackPressed method, but it's not used at all, so BackPressed event is being handled somewhere "higher". So i tried to find out where, but AutoCompleteTextView has no onBackPressed method defined.
Any advices?
You can create your custom AutoCompleteTextView and Override the method onKeyPreIme(int keyCode, KeyEvent event)
I also realized that this method is called 2 times, I'm running my code only in the second time. Here is the example:
#Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
//add your code here
return true;
}
return super.onKeyPreIme(keyCode, event);
}
You may try this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Your back key press logic
}
return true;
}
Remember to return true to prevent this event from being propagated further, or false to indicate that you have not handled this event and it should continue to be propagated.

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