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
Just write on click finish(). It will take you to the previous Activity.
I suggest the NavUtils.navigateUpFromSameTask(), it's easy and very simple, you can learn it from the google developer.Wish I could help you!
Try
Activity#finish()
. This is more or less what the back button does by default.if you want to go to just want to go to previous activity use
OR
if you want to go to second activity or below that use following:
Start the second activity using intent (either use
startActivity
orstartActivityForResult
according to your requirements). Now when user press back button, the current activity on top will be closed and the previous will be shown.Now Lets say you have two activities, one for selecting some settings for the user, like language, country etc, and after selecting it, the user clicks on Next button to go to the login form (for example) . Now if the login is unsuccessful, then the user will be on the login activity, what if login is successful ?
If login is successful, then you have to start another activity. It means a third activity will be started, and still there are two activities running. In this case, it will be good to use
startActivityForResult
. When login is successful, send OK data back to first activity and close login activity. Now when the data is received, then start the third activity and close the first activity by using finish.If you have setup correctly the AndroidManifest.xml file with activity parent, you can use :
Where this is your child activity.