demlik
demlik

Reputation: 29

using onBackPressed in android

I am working on an android application project which has more than 8 activities just up to now. I need to learn how back button behave in android. Because I need to override it and do some actions when it pressed. When back button pressed android looks up the trace file and go to the activity which you came from to the current activity. However in my application you can go to an activity from several other activities. And I should know from where i came to this activity so that i can decide whether I should override onBackPressedmethod or not. But I don't want to do this with carrying some parameters with something like putExtra and startActivity. Is there a better way to handle this problem? Thanks

Upvotes: 0

Views: 443

Answers (3)

user2208695
user2208695

Reputation: 84

Sure override onbackpressed and use intent to traverse to what ever class you want to move.

Upvotes: 1

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

You can find out which activity launch this activity by calling getCallingActivity ()

Upvotes: 0

Rafael Sanches
Rafael Sanches

Reputation: 1823

You could try doing a BaseActivity and putting your custom logic there.

All of your 8 activities would extend that BaseActivity that has custom behavior there.

From that BaseActivity you can do whatever logic you want in onBackPressed based on a custom variable that you would send.

I wouldn't recommend overriding onBackPressed ever. Check the other lifecycle methods: onPause or onDestroy.

If you want to do something like a custom navigation you should check the ActionBar goUp instead.

Upvotes: 0

Related Questions