0

when i click the back button on an android device, the onBackPressed will fire and it will navigate me to the previous activity.

is there a way how i can know which is the previous activity?

thanks in advance

clive

  • This should help you http://developer.android.com/reference/android/app/Activity.html#getParent%28%29 – Tizianoreica Aug 08 '13 at 15:05
  • Are you wanting to know if the previous activity is yours? You can do something like [this](http://stackoverflow.com/questions/8119526/android-get-previous-activity). – thegrinner Aug 08 '13 at 15:06
  • @AntonioCalì if i get the parent of the activity, it wont help me – Clive Cauchi Aug 09 '13 at 08:03
  • @thegrinner the activities in the application are all mine. in other words i am not going to be able to access my application through another application. If i have 2 activities, activity AA and activity BB. if from AA i go to BB and from BB i go to AA again. if i touch the back button, from AA i go to BB. now i need to confirm that if i touch the back button again, i go from BB to AA. i believe that intents will be lost since you are creating new ones all the time. hope i explained myself better now – Clive Cauchi Aug 09 '13 at 08:10
  • @CliveCauchi Have you tried what you described above and what happens? The intents should not be "lost" unless you add some special flags when starting the activities. – Emil Davtyan Aug 09 '13 at 10:17

3 Answers3

1

Assuming you only want to know the name of the previous Activity, you can put the name of the previous Activity in the Intent using putExtra(String name, String value) method. Then you can get the name of the Activity in onCreate of the child Activity by using getStringExtra(String name) method on the Activity's Intent. You can get the intent by using the getIntent() method of the Activity.

Ganesh Bhambarkar
  • 930
  • 1
  • 8
  • 9
1

You could use getCallingActivity if you started the activity to get results.

Note: if the calling activity is not expecting a result (that is it did not use the startActivityForResult(Intent, int) form that includes a request code), then the calling package will be null.

So maybe you can hack it by starting the activities for result without actually providing a result.

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
-2

Maybe You can try this,

super.onBackPressed();
karthik
  • 1
  • 4