0

I have MenuActivity with "new game" and "resume" buttons. "new game" starts FireRoomActivity.

public void newGame(View view){
        Intent intent = new Intent(this, FireRoomActivity.class);
        this.finish();  
        startActivity(intent);
    }

In FireRoomActivity the onBackPressed is overriden to go back to MenuActivity.

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent inMain=new Intent(this, MenuActivity.class);
    startActivity(inMain);
}

The "resume" button in MenuActivity should resume FireRoomActivity.But this just quits app.

public void resume(View view){
    this.finish();
}

Q1) What am I doing wrong?

Q2) If I add third activity "ThreeActivity" and start it from FireRoomActivity(finishing FireRoomActivity), and in this "ThreeActivity" override onBackPressed to go to MenuActivity, how would I go about it?

I'm new to android life cycle, so I would appreciate detailed reply

Nazerke
  • 2,098
  • 7
  • 37
  • 57
  • Activity will follow its own lifecycle, so you can not call onResume Directly. It will call onRestart first when coming from other activity and then followed by other methods – Tofeeq Ahmad Nov 26 '13 at 06:45

5 Answers5

2
**Try This**
======================
public void onBackPressed() {
Intent i = new Intent(getApplicationContext(), MenuActivity.class);
            i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(i);
            finish();
}
ishu
  • 1,322
  • 1
  • 11
  • 15
2

1) You are messing with the natural order. There is no need to call "finish" in the "New game" method.

Just leave the activity on the stack so that you can go back to it when you press the back key. You then do not need to fire up a new activity when the back key is pressed because you already will go back to the one MenuActivity you already have.

Your resume method is calling finish and therefore closing the Menu. Instead of "finish"ing the menu, put in your code to resume the game.

2) Learn the Activity Lifecycle. Once you understand this you will handle this much better. There is no need to finish an activity when you start another one if you are going to be returning to it. Think of them like a stack. You put activities on the stack and the back button pops them back off again. You only finish an activity when you have completely finished with it and never want to see it again.

Kuffs
  • 35,581
  • 10
  • 79
  • 92
  • I'm a little confused. If i don't call finish in MenuActivity, FireRoomActivity will be in the front in the stack as such: **** FireRoomActivity, MenuActivity **** Then, when the user is in FireRoomActivity and presses back button, FireRoomActivity needs to be finished in order to MenuActivity to come forward of the stack. Then, when resume button clicked I need to resume FireRoomActivity, how do I do it? – Nazerke Nov 26 '13 at 08:02
  • You can not resume an activity that is finished. Start a new FireRoomActivity but include Extras that tell it to resume from your saved state. Keep your stack simple. You will have all sorts of issues if you manipulate it the way you are trying to. Above all, learn the Activity Lifecycle. If you understood it, you would not be having these issues. – Kuffs Nov 26 '13 at 10:36
  • 1
    It may help if you try not to think of your activities as permanent game screens. They are windows into your game through which the user interacts. You should be able to save all game state on request (such as when a user pauses), reboot the device but still restart your game activity in the same state that your user left it. – Kuffs Nov 26 '13 at 10:50
  • yeah I didn't completely understand back stacks, that's why I'm asking. I was trying to avoid saving game state in this scenario. Thanks though, some of your advises helped me. upvote for that – Nazerke Nov 26 '13 at 11:29
  • You NEED to be able to save game state. If a user is playing your game and receives a phone call on the same device, they would lose their progress. – Kuffs Nov 26 '13 at 11:33
  • I save game state in that specific case (phone call) and in case if android destroys my app due to memory lack. But for going back to menu or coming from menu to resume I thought may be there is a way to get away without saving state. Because it slows down transition – Nazerke Nov 26 '13 at 12:07
0

Try:

@Override
public void onBackPressed() {
    Intent inMain=new Intent(this, MenuActivity.class);
    startActivity(inMain);
}  

onBackPressed() itself finishes activity. In this case if you press "back" button in FireRoomActivity it will always pause. So if you want to finish it, than you will need to call finish();

Drake29a
  • 896
  • 8
  • 23
0

Remove super.OnBackPressed() in onBackPressed overridden method.

If it doesn't work try this :

Start your FireRoomActivity with the syntax on Clicking of resume button

Intent i = new Intent(getActivity(), FireRoomActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
startActivity(i);
Piyush Agarwal
  • 25,608
  • 8
  • 98
  • 111
0

Overide this in your ThreeActivity

@Override
public void onBackPressed() {
    Intent intent=new Intent(this, MenuActivity.class);
    startActivity(intent);
}

For more info about refer this link

Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138