Search code examples
androidandroid-activity

Stopping an Activity in Android


I had read about this thread around the Internet but unlucky to find a solution. The solutions that are available did not work for me like adding the android:noHistory="true" in the Manifest file. What I wanted is that after the user clicks the play game button, which will redirect the user into the select difficulty page, when the user clicks the back button (android emulator), it will go back to the main menu.

This is my manifest file:

<?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>

This is my main activity:

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();
    }
}

This is my difficulty class:

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);
            }
        });
    }

}

Solution

  • First of all, you should know the difference between startActivity() and startActivityForResult(), see Android developers website.

    Second, I think you should need to understand the lifecycle of an Activity, you don't need to finish() every activity once you leave it, Android will manage that for you.

    Hope it helps :) Cheers!