Go to home screen instead of previous Activity

2020-02-25 08:11发布

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.

7条回答
ゆ 、 Hurt°
2楼-- · 2020-02-25 08:42

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();
}
查看更多
欢心
3楼-- · 2020-02-25 08:48

You could also simply call the finish() method on the activity.

查看更多
Animai°情兽
4楼-- · 2020-02-25 08:49

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();
    }
}
查看更多
▲ chillily
5楼-- · 2020-02-25 08:56
      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);
                }
            });
查看更多
Ridiculous、
6楼-- · 2020-02-25 08:57

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);
}
查看更多
时光不老,我们不散
7楼-- · 2020-02-25 08:58

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.

查看更多
登录 后发表回答