0

Assume I have two activities.
Activity A and Activity B, while Activity A is main activity.
There is a button in Activity A that will go to Activity B.
So, Activity A -> click -> Activity B and then click back button, it will go back to Activity A.
However, there is another story that I will have a GCM notification.
When clicking the notification, it will immediately go to Activity B.
But the problem is:

  1. If the application already run, when I click the notification and go to Activity B, and then click back button, it will correctly go back to Activity A.
  2. If I kill the application, and click the notification and go to Activity B, and then click back button, it will go to the Home Screen. But the problem is I want it will go back to Activity A.

How to solve this problem?

Shruti
  • 1
  • 13
  • 55
  • 95
jjLin
  • 3,281
  • 8
  • 32
  • 55
  • This might confuse the users. Goes against the Android "Pattern" . You could provide a separate button to go to Activity A? And let the Back Press do what it does. – Sameer Aug 24 '12 at 09:46
  • override onBackpressed in your Activity B and start your activity A from there then it will work in both use cases. – Sankar Aug 24 '12 at 09:46

2 Answers2

2

Override you onBackPressed method and start the activity A using an intent.

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
        // Start Activity A heres
}
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
  • if Activity A already created, will it have some problems if I start the activity again? – jjLin Aug 24 '12 at 10:06
  • You can kill Activity A when moving to Activity B to ensure that you wont have that extra Activity. Just add finish(); after startActivity(). – Lazy Ninja Aug 24 '12 at 10:13
  • There is another way to find out the running activities too. You can try to see if Activity A is running or not. – Lazy Ninja Aug 24 '12 at 10:14
  • but the case is that I want to go back to Activity A if it exists, otherwise just create it. How to check? – jjLin Aug 24 '12 at 10:23
  • There are different ways to do it as listed here http://stackoverflow.com/questions/5446565/android-how-do-i-check-if-activity-is-running. Like using a static value, .... – Lazy Ninja Aug 24 '12 at 10:38
0
@Override
public void onBackPressed() {
// Don't forget to remove super.onBackPressed()
startActivity(new Intent(ActivityB.this,ActivityA.class));

}
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109