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.
You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above code, we have given text view, when the user click on text view, it will open new activity.
Simply if you go to back Home activity than add this code
@Override
public void onBackPressed()
{
Intent intent=new Intent(currentactivity.this,Mainactivity.class);
startActivity(intent);
finish();
}// on back Pressed first add activity where you stand and add activity where you go
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