如何重新启动一个Android活动 [英] How do I restart an Android Activity

查看:147
本文介绍了如何重新启动一个Android活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重新启动一个Android 活动?我尝试以下,但活动干脆退出。

 公共静态无效restartActivity(活动行为){

        意向意图=新的意图();
        intent.setClass(动作,act.getClass());
        act.startActivity(意向);
        act.finish();

}
 

解决方案

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

 意向意图= getIntent();
完();
startActivity(意向);
 

基本上,我打电话完成()第一,而我使用的是完全相同的意图这项活动开始。这似乎这样的伎俩?

更新:正如拉尔夫以下,<一个href="http://developer.android.com/reference/android/app/Activity.html#recreate%28%29"><$c$c>Activity.recreate()是的路要走API 11和超越。这是preferable,如果你在一个API11 +环境。您还可以查看当前的版本,并呼吁,如果你在API 10或低于上述code段。 (请别忘了upvote拉尔夫的答案!)

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

public static void restartActivity(Activity act){

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

}

解决方案

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?

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!)

这篇关于如何重新启动一个Android活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆