简体   繁体   English

使用intent重新启动当前活动

[英]Re-launch current activity using intent

For an App I am developing, I want to re-launch the current activity using an intent. 对于我正在开发的应用程序,我想使用intent重新启动当前活动。 So I'm in MainActivity.class and I want to re-launch MainActivity.class using the following: 所以我在MainActivity.class中,我想使用以下内容重新启动MainActivity.class:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);

This calls onDestroy() but does not re-launch the activity. 这会调用onDestroy()但不会重新启动该活动。 Why doesn't this work? 为什么这不起作用?

If your are in an Activity: this.recreate(); 如果你在一个Activity中: this.recreate();

If your are in a Fragment: getActivity.recreate(); 如果你在片段中: getActivity.recreate();

Related Links : 相关链接 :

How do I restart an Android Activity 如何重新启动Android活动

how do I restart an activity in android? 如何重启android中的活动?

Android activity restart Android活动重启

You could just use: 你可以使用:

finish();
startActivity(getIntent());

Which will finish the current activity, and start a new activity with the same intent that you received when the activity was originally created. 这将完成当前活动,并以与最初创建活动时收到的相同意图开始新活动。 This should effectively re-launch the activity as is. 这应该有效地重新启动活动。

Edit: 编辑:

See Reload activity in Android 请参阅Android中的重新加载活动

包括这一个......

startActivity(intent); 

Just do this: 这样做:

  Intent i=getIntent();//This simply returns the intent in which the current Activity is started
  finish();//This would simply stop the current Activity.
  startActivity(i);//This would start a new Activity.

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

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