Question

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

Was it helpful?

Solution 2

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.

OTHER TIPS

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top