How to make device back button cause onCreate? - android

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.

You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}

Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Related

How to detect "last" back press?

I have an Activity with Fragment, and there is a lot of transactions, so the back button most often just roll back Fragment transaction. But I need to detect the last back press.
I can't use onPause method, because I don't want to do anything when the user presses home, or starts another activity.
Use this getFragmentCount method below.
It will return the Fragment count if it is 0 then your are in last back press
private int getFragmentCount() {
return getSupportFragmentManager().getBackStackEntryCount();
}
call this method in your onBackPressed() of your Activity
Don't know if this helps, but here is the code to catch if the backbutton was pressed:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(keyCode ==KeyEvent.KEYCODE_BACK)
{
//Do stuff here
}
return super.onKeyDown(keyCode, event);
}

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

How to catch event with hardware back button on android?

How to catch event with hardware back button on android ? I need to supress user to go back and I when click on back button on phone to show message and not to go on previous activity. How to do that ?
you can do by this
override the onBackPressed() method into your Activity like this way
public void onBackPressed(){
// do something here and don't write super.onBackPressed()
}
override the onKeyDown() method
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event);
}
Override the method onBackPressed() in whatever Activity you want to create a different behaviour to the back button.
These question are equal to yours (and could have been found by a simple search):
how to disable back button in android
Disable back button in android
You can suppress user "back" action with onKeyDown() in your activity like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//do actions like show message
return false;
}
return super.onKeyDown(keyCode, event);
}

Manage the back button with multiple scene

I followed this tutorial http://www.andengine.org/forums/tutorials/multiple-screen-andengine-game-v2-t4755.html to create a simple application with multiple scene and only one activity.
I'd like to know how can i can return to the previous scene when i use the back button and finish the activity when i'm in the first scene.
I tried so in the MultiScreen Class:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
this.mEngine.getScene().back();
}
return super.onKeyDown(keyCode, event);
}
replacing the core.getEngine().setScene(scene); in the SceneManager with this.mEngine.getScene().setChildScene(scene);
scene work differently from how I understood, I resolve with:
#Override
public void onBackPressed()
{
Scene scene = this.mEngine.getScene();
if(scene.hasChildScene()){
scene.back();
}
else{
this.finish();
}
}
You can override the back key in one of two ways, either by overriding the onBackPressed() method, or the dispatchKeyEvent() method
Overriding onBackPressed:
#Override
public void onBackPressed()
{
// your code here
}
Overriding dispatchKeyEvent:
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
// your code here
}
return (yourbooleanhere);
}
I had a dilemma like you in that sense. I explain. I have an activity like works with a ViewFlipper. I need to go to the different "layout" that I wrote on ViewFlipper tag and, when a I was in a particular "layout" that is not the main "layout" of the activity, I need to come back to the main pressing the back button, but, it doesn't work what I want.
To resolve this dilema, I override OnBackPressed function like:
#Override
public void onBackPressed() {
if (condition) {
// go to the main
} else {
super.onBackPressed();
}
}
where condition, as name says, how do you know that you are in the main "layout"? For example, in my ViewFlipper, the main "layout" are ordered by numbers, that begin on 0, so my main "layout" is 0 and how I know that I am in the 0 "layout", viewflipper.getDisplayedChild(). If it returns 0, I stay in the main "layout" or not, otherwise.
So simple but, I know, not so elegant. It's an idea, I think that can help you.

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