【问题标题】:Re-launch current activity using intent使用意图重新启动当前活动
【发布时间】:2023-03-24 19:02:02
【问题描述】:

对于我正在开发的应用程序,我想使用意图重新启动当前活动。所以我在 MainActivity.class 中,我想使用以下命令重新启动 MainActivity.class:

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

这会调用onDestroy(),但不会重新启动活动。为什么这不起作用?

【问题讨论】:

标签: android android-intent android-activity launch


【解决方案1】:

如果您在 Activity 中: this.recreate();

如果您在 Fragment 中: getActivity.recreate();

相关链接:

How do I restart an Android Activity

how do I restart an activity in android?

Android activity restart

【讨论】:

    【解决方案2】:

    你可以使用:

    finish();
    startActivity(getIntent());
    

    这将完成当前活动,并以最初创建活动时收到的相同意图启动新活动。这应该有效地重新启动活动。

    编辑:

    Reload activity in Android

    【讨论】:

      【解决方案3】:

      这样做:

        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.
      

      【讨论】:

        【解决方案4】:

        包括这个...

        startActivity(intent); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-15
          相关资源
          最近更新 更多