停止Android活动

时间:2011-10-28 09:29:12

标签: android android-activity

我已经在互联网上阅读了这个帖子,但不幸的是找到了解决方案。可用的解决方案对我来说不起作用,比如在清单文件中添加android:noHistory="true"。我想要的是,在用户点击playgame按钮后,将用户重定向到selectdifficulty页面,当用户点击后退按钮(android模拟器)时,它将返回到mainMenu。

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.kfc"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".NewKFCActivity"
              android:label="@string/app_name"
              android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".HelpPageOne"
              android:label="@string/app_name"
              android:noHistory="true">
        <intent-filter>
            <action android:name="com.kfc.HELPPAGEONE" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name=".HelpPageTwo"
              android:label="@string/app_name"
              android:noHistory="true">
        <intent-filter>
            <action android:name="com.kfc.HELPPAGETWO" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name=".SelectDifficulty"
              android:label="@string/app_name"
              android:noHistory="true">
        <intent-filter>
            <action android:name="com.kfc.SELECTDIFFICULTY" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

</application>

这是我的主要活动:

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

public class NewKFCActivity extends Activity {
    /** Called when the activity is first created. */
    ImageButton bPlay, bHelp;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        bPlay = (ImageButton) findViewById(R.id.playGame);
        bPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(v.getContext(), SelectDifficulty.class);
                startActivityForResult(intent,0);
                finish();
            }
        });

        bHelp = (ImageButton) findViewById(R.id.Help);
        bHelp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(v.getContext(), HelpPageOne.class);
                startActivityForResult(intent,0);
                finish();
            }
        });


    }
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        finish();
    }
}

这是我的难度等级:

package com.kfc;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class SelectDifficulty extends Activity{
    ImageButton bEasy, bMedium, bHard;
    @Override
    protected void onCreate(Bundle kfcState) {
        // TODO Auto-generated method stub
        super.onCreate(kfcState);
        setContentView(R.layout.difficultyxml);

        bEasy = (ImageButton) findViewById(R.id.easy);
        bEasy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //Intent intent = new Intent(v.getContext(), NewKFCActivity.class);
                //startActivityForResult(intent,0);
            }
        });
    }

}

2 个答案:

答案 0 :(得分:3)

首先,您应该了解startActivity()startActivityForResult()之间的区别,请参阅Android developers website

其次,我认为您应该了解lifecycle of an Activity,一旦离开,您就不需要finish()每个活动,Android会为您管理。

希望有帮助:) 干杯!

答案 1 :(得分:0)

删除行:

finish();

startActivityForResult(...);

Activity NewKFCActivity,因为finish()正在破坏您的活动,当您按下时,没有更多活动可以返回!