Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to home screen instead of previous Activity

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.

like image 847
reiley Avatar asked Aug 04 '12 09:08

reiley


People also ask

How do I go back to previous 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.

How do I go back to first activity on Android?

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.


1 Answers

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
like image 186
abubakar waqas Avatar answered Oct 21 '22 18:10

abubakar waqas