如何在 Android 中重新启动 Activity

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1397361/
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 03:01:58  来源:igfitidea点击:

How to restart Activity in Android

androidandroid-activity

提问by EboMike

How do I restart an Android Activity? I tried the following, but the Activitysimply quits.

如何重启安卓Activity?我尝试了以下,但Activity只是退出。

public static void restartActivity(Activity act){

        Intent intent=new Intent();
        intent.setClass(act, act.getClass());
        act.startActivity(intent);
        act.finish();

}

回答by EboMike

I did my theme switcher like this:

我做了这样的主题切换器:

Intent intent = getIntent();
finish();
startActivity(intent);

Basically, I'm calling finish()first, and I'm using the exact same intent this activity was started with. That seems to do the trick?

基本上,我finish()先打电话,我使用的意图与此活动开始时完全相同。这似乎行得通?

UPDATE: As pointed out by Ralf below, Activity.recreate()is the way to go in API 11 and beyond. This is preferable if you're in an API11+ environment. You can still check the current version and call the code snippet above if you're in API 10 or below. (Please don't forget to upvote Ralf's answer!)

更新:正如下面 Ralf 所指出的,Activity.recreate()是 API 11 及更高版本的方法。如果您在 API11+ 环境中,这是更可取的。如果您使用的是 API 10 或更低版本,您仍然可以检查当前版本并调用上面的代码片段。(请不要忘记为拉尔夫的回答点赞!)

回答by Ralf

Since API level 11 (Honeycomb), you can call the recreate()method of the activity (thanks to thisanswer).

从 API 级别 11 (Honeycomb) 开始,您可以调用活动的recreate()方法(感谢这个答案)。

The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.

recreate() 方法就像配置更改一样,因此如果适用,也会调用 onSaveInstanceState() 和 onRestoreInstanceState() 方法。

回答by Ben

Before SDK 11, a way to do this is like so:

在 SDK 11 之前,这样做的方法是这样的:

public void reload() {
    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);
    startActivity(intent);
}

回答by JustinMorris

Just to combine Ralf and Ben's answers (including changes made in comments):

只是为了结合拉尔夫和本的答案(包括评论中所做的更改):

if (Build.VERSION.SDK_INT >= 11) {
    recreate();
} else {
    Intent intent = getIntent();
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);

    startActivity(intent);
    overridePendingTransition(0, 0);
}

回答by Thomas Vos

I used this code so I still could support older Android versions and use recreate()on newer Android versions.

我使用了这段代码,所以我仍然可以支持旧的 Android 版本并recreate()在较新的 Android 版本上使用。

Code:

代码:

public static void restartActivity(Activity activity){
    if (Build.VERSION.SDK_INT >= 11) {
        activity.recreate();
    } else {
        activity.finish();
        activity.startActivity(activity.getIntent());
    }
}

Sample:

样本:

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    private Activity mActivity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mActivity = MainActivity.this;

        Button button = (Button) findViewById(R.id.restart_button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restartActivity(mActivity);
            }
        });
    }

    public static void restartActivity(Activity activity) {
        if (Build.VERSION.SDK_INT >= 11) {
            activity.recreate();
        } else {
            activity.finish();
            activity.startActivity(activity.getIntent());
        }
    }
}

回答by user3748515

This solution worked for me.

这个解决方案对我有用。

First finish the activity and then start it again.

首先完成活动,然后重新开始。

Sample code:

示例代码:

public void restartActivity(){
    Intent mIntent = getIntent();
    finish();
    startActivity(mIntent);
}

回答by Abhishek Soni

This is by far the easiest way to restart the current activity:

这是迄今为止重新启动当前活动的最简单方法:

finish();
startActivity(getIntent());

回答by Nikhil Dinesh

Call this method

调用这个方法

private void restartFirstActivity()
 {
 Intent i = getApplicationContext().getPackageManager()
 .getLaunchIntentForPackage(getApplicationContext().getPackageName() );

 i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK );
 startActivity(i);
 }

Thanks,

谢谢,

回答by Chris.Jenkins

Even though this has been answered multiple times.

尽管这已被多次回答。

If restarting an activity from a fragment, I would do it like so:

如果从片段重新启动活动,我会这样做:

new Handler().post(new Runnable() {

         @Override
         public void run()
         {
            Intent intent = getActivity().getIntent();
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
            getActivity().overridePendingTransition(0, 0);
            getActivity().finish();

            getActivity().overridePendingTransition(0, 0);
            startActivity(intent);
        }
    });

So you might be thinking this is a little overkill? But the Handlerposting allows you to call this in a lifecycle method. I've used this in onRestart/onResumemethods when checking if the state has changed between the user coming back to the app. (installed something).

所以你可能会认为这有点矫枉过正?但是Handler发布允许您在生命周期方法中调用它。在检查用户返回应用程序之间的状态是否发生变化时,我在onRestart/onResume方法中使用了它。(安装了一些东西)。

Without the Handlerif you call it in an odd place it will just kill the activity and not restart it.

如果没有,Handler如果你在一个奇怪的地方调用它,它只会杀死活动而不是重新启动它。

Feel free to ask any questions.

随意问任何问题。

Cheers, Chris

干杯,克里斯

回答by Codeversed

Well this is not listed but a combo of some that is already posted:

好吧,这没有列出,而是已经发布的一些组合:

if (Build.VERSION.SDK_INT >= 11) {
    recreate();   
} else {
    Intent intent = getIntent();
    finish();
    startActivity(intent);
}