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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11807554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:55:41  来源:igfitidea点击:

Go to home screen instead of previous Activity

androidandroid-activity

提问by reiley

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 Activitywhich 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.

我有一个Activity可以从许多不同的活动中调用的。但是我希望当用户按下后退按钮时,而不是之前的活动,应用程序应该转到主屏幕。

One way to use StartActivityFromResult()but then I'll have to use it in every calling Activity.

一种使用方法,StartActivityFromResult()但随后我必须在每次调用 Activity 时使用它。

回答by android

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

您可以onBackPressed()按如下方式覆盖方法。它应该工作。

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

回答by Luke Taylor

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

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

回答by Abhijit Chakra

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()在代码下方。

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

回答by abubakar waqas

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

回答by JoxTraex

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

只需将 Intent 直接发送回家即可。这可以通过设置该意图的操作和类别来完成。

Check the documentation on Intent for details.

有关详细信息,请查看 Intent 上的文档。

回答by Abhishek Raghuvanshi

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_Fragmentevery 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.

如果您有一个 Activity A 并且您制作了 3 个片段,例如 B、C 和Home_Fragment那就很简单了。现在,如果您在片段 B 或 C 中并按后退按钮,您希望每次都在Home_Fragment上移动。
然后,您只需覆盖onBackPressed()Activity A 中的方法,并且当您跳转到任何片段时,然后给出特定的TAG 或 name,您将在 Activity 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();
    }
}

回答by shassss

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