I know this question has been asked many times before, but any of the solution is not working and my situation is a little bit different.
I've an Activity
which can be called from many different Activities. But I want when the user presses back button, instead of previous activity, app should go to Home screen.
One way to use StartActivityFromResult()
but then I'll have to use it in every calling Activity.
And Adding to this question if you dont want to back your activity where you pressed back button then just a
finish()
below the code.You could also simply call the
finish()
method on the activity.It is just simple if you have an Activity A and you make 3 fragments like B, C and Home_Fragment. Now, if you are in fragment B or C and press back button you want to move on Home_Fragment every time.
Then you have to just override the
onBackPressed()
method in Activity A and also when you jump to any fragment, then give a specific TAG or name, you will recognize that fragment in Activity A.I am giving the example that you can easily understand:
Moving from activity A to Fragment C
or if you are moving from fragment B to fragment C, and on back press, you want to come on Fragment D, do like below:
Now you have to just override the onBackPressed() method in main activity as follows:
you can override
onBackPressed()
method as follows. It should work.Just send an Intent directly home. This can be done through setting the action and category of that intent.
Check the documentation on Intent for details.