【问题标题】:Finishing old activity with noHistory使用 noHistory 完成旧活动
【发布时间】:2013-07-29 20:32:14
【问题描述】:

我试图理解完成一项活动并开始另一项活动。

<activity
            android:name="com.blabla.game.OyunActivity"
            android:label="@string/title_activity_oyun"
            android:noHistory="true" >
        </activity>

OyunActivity:

int number = 1;
while(true)
{
if(number == 52)
{
            Intent intent = new Intent(this, GameOver.class);
            startActivity(intent);
            finish();
}
number++;
Log.d("TAG", number);
}

它正在启动 GameOver 活动,但 OyunActivity 未完成。它保持增加数变量并将其输出到Logcat。

PS : 实际上我的代码并没有像这样愚蠢。我正在尝试制作一个基本游戏。当 number = 52 时,它应该停止并打开 GameOver 活动

【问题讨论】:

  • while(true) 表示您的循环将无限期运行!
  • @Ty221,我知道。我想立即完成()一个活动的所有过程并开始另一个。
  • 如果认为更好的解决方案是:while(number != 52)
  • 我无法使用它。因为实际上我的代码没有任何 while 循环 :) 在这里使用它来简化问题。

标签: android android-intent android-activity


【解决方案1】:

应该是break 而不是finish()

 int number = 1;
while(true)
{
if(number == 52)
{
            Intent intent = new Intent(this, GameOver.class);
            startActivity(intent);
            return;
}
number++;
Log.d("TAG", number);
}

【讨论】:

  • 这次它只会中断while循环,并从外部循环继续?
  • 它将继续完成方法中的所有代码,然后启动GameOver。如果您不想继续使用其他代码,请使用 return 而不是 break。
  • 可能返回;将解决我的问题。我会尝试并分享结果。
  • 已解决。 @Hoan 你能编辑你的答案吗?我会接受的。谢谢。
【解决方案2】:

This question 对你有帮助,我想。

报价:

You can use finish() method or you can use:

android:noHistory="true"

And then there is no need to call finish() anymore.

<activity android:name=".ClassName" android:noHistory="true" ... />

【讨论】:

  • 你看我的问题了吗?请看我问题的第 5 行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
相关资源
最近更新 更多