简体   繁体   English

转到主屏幕而不是之前的活动

[英]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. 我有一个Activity ,可以从许多不同的活动来调用。 But I want when the user presses back button, instead of previous activity, app should go to Home screen. 但是我希望当用户按下后退按钮而不是之前的活动时,app应该转到主屏幕。

One way to use StartActivityFromResult() but then I'll have to use it in every calling Activity. 一种使用StartActivityFromResult()但是我必须在每个调用Activity中使用它。

you can override onBackPressed() method as follows. 您可以按如下方式覆盖onBackPressed()方法。 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. 如果您不希望在按下按钮的情况下返回您的活动,那么只需在代码下方finish()一个finish()

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 如果您回到Home活动,只需添加此代码即可

@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. 只需将Intent直接发送回家。 This can be done through setting the action and category of that intent. 这可以通过设置该意图的动作和类别来完成。

Check the documentation on Intent for details. 有关详细信息,请查看Intent上的文档。

It is just simple if you have an Activity A and you make 3 fragments like B, C and Home_Fragment . 如果你有一个活动A并且你制作了3个片段,如B,C和Home_Fragment ,这很简单。 Now, if you are in fragment B or C and press back button you want to move on Home_Fragment every time. 现在,如果您在片段B或C中并按下后退按钮, 每次都要移动Home_Fragment
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. 然后你必须覆盖活动A中的onBackPressed()方法,当你跳转到任何片段,然后给出一个特定的TAG或名称时 ,你将识别活动A中的那个片段。

I am giving the example that you can easily understand: 我举一个你可以很容易理解的例子:

Moving from activity A to Fragment C 从活动A转移到碎片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: 或者如果你从片段B移动到片段C,并且在背面按下,你想要进入片段D,如下所示:

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: 现在你必须在主活动中覆盖onBackPressed()方法,如下所示:

@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);
                }
            });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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