繁体   English   中英

游戏无法正确退出

[英]Game doesn't Quit properly

到目前为止,我有一个包含两个活动的应用程序:

  • 主菜单活动。

  • 游戏活动

主菜单活动包含一个使用以下代码启动游戏活动的按钮:

public void onClick(View clickedButton)
    {
        switch(clickedButton.getId())
        {
        case R.id.buttonPlay:
            Intent i = new Intent("apple.banana.BouncingBallActivity");
            startActivity(i);
            break;
    }

当用户完成“游戏活动”时,他按下“后退”按钮。 这首先调用onPause()方法,该方法将暂停游戏的动画线程。 然后,它调用onStop(),该onStop()完全在活动上调用finish()。 用户返回到“主菜单”活动。 该代码概述如下:

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启动应用程序时才有效。 当我单击应用程序图标时,游戏从“游戏活动”开始。 主菜单活动不会出现。

我不清楚为什么会发生这种情况。 这可能与清单有关。 我粘贴了以下相关部分:

<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启动器界面)中从游戏活动中获取的原因是它从未离开过那里。

如果您只想在用户按下Back键时结束游戏,而不是在其他暂停时结束游戏,那么您需要捕获传入的键,如果键为Back,则需要finish()

当您按下后退按钮时,您可以执行此操作。

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {       
            this.finish();
        }
        return super.onKeyDown(keyCode, event);
    }

要么

@Override
public void onBackPressed() {
    this.finish();
}

暂无
暂无

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

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