0

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

  • 主菜单活动。

  • 游戏活动

Main Menu Activity 包含一个使用以下代码启动 Game Activity 的按钮:

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

当用户完成游戏活动时,他按下后退按钮。这首先调用了 onPause() 方法,该方法暂停了游戏的动画线程。然后它调用 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>

我真的很感激这方面的任何帮助。谢谢。

4

2 回答 2

1

调用finish()不属于您的onStop()方法。如果您想在用户按下后结束游戏,请将其放在您的onPause(). 应用程序在后续启动时(通过 Android 启动器界面)从游戏活动中获取信息的原因是它从未离开过那里。

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

于 2012-03-29T11:35:58.240 回答
0

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

@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();
}
于 2012-03-29T11:39:37.843 回答