I want to do something simple on android app. How is it possible to go back to a previous activity.
What code do I need to go back to previous activity
I want to do something simple on android app. How is it possible to go back to a previous activity.
What code do I need to go back to previous activity
and if you want on button click go back then simply put
All new activities/intents by default have back/previous behavior, unless you have coded a
finish()
on the calling activity.From your
FirstActivity
call theSecondActivity
usingstartActivityForResult()
methodFor example:
In your
SecondActivity
set the data which you want to return back toFirstActivity
. If you don't want to return back, don't set any.For example: In
secondActivity
if you want to send back data:If you don't want to return data:
Now in your
FirstActivity
class write following code for theonActivityResult()
method.Just this
You can explicitly call
onBackPressed
is the easiest wayRefer Go back to previous activity for details
Add this in your onCLick() method, it will go back to your previous activity
or You can use this. It worked perfectly for me