Back button stuck in endless loop? - android

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

Related

Home Button press issue on my app

I'm trying to solve this but couldn't find any answer on SO. My issue is that whenever I'm in my app and I press the home button, my app layout becomes for a second like the wallpaper for my menu on the phone!
Something like this :
I've tried to override the menu button like this:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
this.finish();
}
return super.onKeyDown(keyCode, event);
}
But this will not work. Any help will be appreciated.

Android: Override onKeyUp and onKeyDown in Intent.ACTION_VIEW

I have an Android tabbed application that displays music charts (say, ChartActivity), each of which in turn displays a list of music tunes (say, EntryActivity). Tapping on each entry of music tunes will launch, e.g., a YouTube app (default actions). That is, in list.setOnItemClickListener of EntryActivity, I make calls:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(selectedSongUrl));
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("YouTube", intent);
(Yes, I use ActivityGroup and TabActivity.)
The problem is that when I tab the back button from the YouTube screen, the active view does not come back to EntryActivity, but to ChartActivity. I guess, this is because I could not override onKeyUp and onKeyDown in YouTube screen so that I can further pass the event to the parent activity:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
// Overrides the default implementation for KeyEvent.KEYCODE_BACK
// so that all systems call onBackPressed().
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
Is there any way to let EntryActivity know callback events sent from Intent.ACTION_VIEW?
I'm a little confused on the setup of your app... Is this a traditional tabbed activity where each tab displays its own activity (e.g. ChartActivity and EntryActivity are tabs)?
If so, you could do the following in your TabGroupActivity:
Override onPause() to save the index of the currently active tab in SharedPreferences.
Override onResume() to read in that preference (if it exists) and switch to the tab that you were last in.
Override onDestroy() and remove that preference so the next time you launch the activity you start in the correct tab.
If that is not how your activity is set up, please explain a little more about how things are set up.

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

Problem in using keyboard's Back button in Android

I am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity

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