简体   繁体   中英

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.

you can override onBackPressed() method as follows. It should work.

public void onBackPressed() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(startMain);
}

您也可以简单地在活动上调用finish()方法。

And Adding to this question if you dont want to back your activity where you pressed back button then just a finish() below the code.

public void onBackPressed() {
     Intent mainActivity = new Intent(Intent.ACTION_MAIN);
     mainActivity.addCategory(Intent.CATEGORY_HOME);
     mainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     startActivity(mainActivity);
     finish();
}

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

Just send an Intent directly home. This can be done through setting the action and category of that intent.

Check the documentation on Intent for details.

It is just simple if you have an Activity A and you make 3 fragments like B, C and Home_Fragment . Now, if you are in fragment B or C and press back button you want to move on Home_Fragment every time.
Then you have to just override the onBackPressed() method in Activity A and also when you jump to any fragment, then give a specific TAG or name , you will recognize that fragment in Activity A.

I am giving the example that you can easily understand:

Moving from activity A to Fragment C

if (savedInstanceState == null) {
    getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new C_fragment(),"xyz").commit();
}

or if you are moving from fragment B to fragment C, and on back press, you want to come on Fragment D, do like below:

btn.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View view) {
        getActivity().getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new C_frament(), "xyz").commit();
        ((ActionBarActivity) getActivity()).getSupportActionBar().setTitle("Fragment C");
    }
});

Now you have to just override the onBackPressed() method in main activity as follows:

@Override
public void onBackPressed() {
    FragmentManager fragmentManager =getSupportFragmentManager();

    if (((C_fragment) getSupportFragmentManager().findFragmentByTag("xyz")) != null 
            && ((C_fragment) getSupportFragmentManager().findFragmentByTag("xyz")).isVisible()) {

        Fragment fragment = new Home_Fragment();
        fragmentManager.beginTransaction()
                .replace(R.id.container, fragment)
                .commit();
        getSupportActionBar().setTitle("Home fragment ");

    } else {
        super.onBackPressed();
    }
}
      Button btn = (Button) findViewById(R.id.button2);
         btn.setOnClickListener(new OnClickListener() {
                public void onClick(View v) 
                {
          Intent i = new Intent(AccountActivity.this, HomeActivity.class);
                    startActivity(i);
                }
            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM