I'm going from main activity to another activity and then when I press back button I want to go back to the main activity and restart it. How can I do that?
You could just override the onBackButton pressed in your second activity to start the first one. Example:
@Override
public void onBackPressed() {
startActivity(new Intent(this, FirstActivity.class));
}
When you press the back button while in the second activity, then Android automatically invokes the previous activity on the back stack, which is in your case your main activity.
For more information, see the Android docs for Tasks and Back Stack.
Android calls the onResume() callback every time you return to an activity with the back button. Do all your "restart" work there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With