我有一个应用程序,到目前为止包含两个 Activity :
主菜单 Activity 。
游戏 Activity
主菜单 Activity 包含一个使用以下代码启动游戏 Activity 的按钮:
public void onClick(View clickedButton)
{
switch(clickedButton.getId())
{
case R.id.buttonPlay:
Intent i = new Intent("apple.banana.BouncingBallActivity");
startActivity(i);
break;
}
当用户完成游戏 Activity 时,他按下后退按钮。这首先调用 onPause() 方法,该方法暂停游戏的动画线程。然后它调用 onStop(),后者对 Activity 完全调用 finish()。用户返回到主菜单 Activity 。代码概述如下:
public class BouncingBallActivity extends Activity{
private BouncingBallView bouncingBallView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bouncingBallView = new BouncingBallView(this);
bouncingBallView.resume();
setContentView(bouncingBallView);
}
@Override
protected void onPause()
{
super.onPause();
bouncingBallView.pause();
}
@Override
protected void onResume()
{
super.onResume();
bouncingBallView.resume();
}
@Override
protected void onStop()
{
super.onStop();
this.finish();
}
}
问题是,只有当我从 Eclipse 启动应用程序时,这才有效。 当我单击应用程序图标时,游戏从游戏 Activity 开始。主菜单 Activity 不出现。
我不清楚为什么会发生这种情况。这可能与 list 有关。我已粘贴以下相关部分:
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".BouncingBallActivity"
android:label="@string/app_name"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="apple.banana.BouncingBallActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".MainMenu"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我非常感谢任何对此的帮助。谢谢。
最佳答案
对 finish()
的调用不属于您的 onStop()
方法。如果您想在用户按回键时结束游戏,请将其放在 onPause()
中。应用程序在后续启动时(通过 Android 启动器界面)从游戏 Activity 中获取的原因是它从未离开过那里。
如果您只想在用户按下“后退”键时结束游戏,而不是从其他暂停中结束游戏,那么您需要捕获传入的按键,并在按键为“后退”时finish()
.
关于java - 游戏无法正常退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9924422/