以编程方式重新启动/重新创建活动?

问题描述:

在我做我的数据库中的一些变化,这涉及到我的观点显著的变化,我想重绘,重新执行的onCreate。

After I do some change in my database, that involves significant change in my views, I would like to redraw, re-execute onCreate.

这怎么可能?

嗯,也许这是不是做的事情(根据CommonsWare的评论)的一个很好的方式,但我处理这种情况在我的比赛方式是简单地重复使用启动该​​活动的意图。定义一个意图 starterIntent 在类的顶部,并在onCreate方法使用 starterIntent = getIntent()检索意图开始了这项活动。然后,当你要重新启动该活动,请拨打结束(); startActivity(starterIntent);

Hmm, maybe this isn't a good way of doing things (according to CommonsWare's comment), but the way I've handled that situation in my game is simply reusing the intent that started the activity. Define an intent starterIntent at the top of your class and in the onCreate method use starterIntent = getIntent() to retrieve the Intent that started this activity. Then when you want to restart the activity, call finish(); startActivity(starterIntent);

就像我上面写的,也许这并不是一个很优雅的解决方案和废物的处理时间,但它是一个简单的方法来重新启动活动,并迫使它重新加载一切。

Like I wrote above, perhaps this isn't a very elegant solution and wastes processing time, but it's a simple way to restart your activity and force it to reload everything.