going back an activity

I can go to my SoundActivity class from my mainActivity, which is a menu. However when i click the device back button it does not go back to my mainActivity instead it just closes the app. I have put finish() at the end but that hasn't seemed to work. I have also tried the super.backPressed() and that hasn't worked either. This is the code for the sound class

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.ToggleButton;

 public class SoundActivity extends Activity
    {
MediaPlayer ourSong;

private SeekBar volumeSeekbar = null;
private ToggleButton muteButton = null;
private AudioManager audioManager = null;

private AudioManager mAm;
private boolean mIsMute;
@Override 
protected void onCreate(Bundle savedInstanceState) 
{    
    // TODO Auto-generated method stub     
    super.onCreate(savedInstanceState);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);
    setContentView(R.layout.soundmenu);
    initControls();


    ourSong = MediaPlayer.create(SoundActivity.this, R.raw.beat2);
    ourSong.start();

}



private void initControls()
{
    try
    {
        volumeSeekbar = (SeekBar)findViewById(R.id.sbVolumeBar);
        audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        volumeSeekbar.setMax(audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        volumeSeekbar.setProgress(audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC));   



        volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
        {
            public void onStopTrackingTouch(SeekBar arg0) 
            {
            }

            public void onStartTrackingTouch(SeekBar arg0) 
            {
            }

            public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
            {
                audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                        progress, 0);
            }
        });

        muteButton = (ToggleButton)findViewById(R.id.toggleButton1);
        muteButton.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) {
                // TODO Auto-generated method stub
            if(muteButton.isChecked())
            {
                AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                  am.setStreamMute(AudioManager.STREAM_MUSIC, true);
            }
            else
            {
                AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
                  am.setStreamMute(AudioManager.STREAM_MUSIC, false);
            }
        }
        });
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}



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

    }
}

If anyone could help me i would be really grateful. Confused at the moment

ok heres the mainActivity that starts sound activity

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ToggleButton;
import android.os.Bundle;

public class PinballShooterActivity extends Activity {
/** Called when the activity is first created. */
MediaPlayer ourSong;

private ToggleButton muteButton = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);



    ourSong = MediaPlayer.create(PinballShooterActivity.this, R.raw.beat2);
    ourSong.start();

    Button soundBtn = (Button) findViewById(R.id.sound); 
    soundBtn.setOnClickListener(new View.OnClickListener() {



    public void onClick(View v) { 
        // TODO Auto-generated method stub             
        startActivity(new Intent(PinballShooterActivity.this, SoundActivity.class));         
        }     
    });  

    Button highScrBtn = (Button) findViewById(R.id.highscores); 
    highScrBtn.setOnClickListener(new View.OnClickListener() {



    public void onClick(View v) { 
        // TODO Auto-generated method stub             
        startActivity(new Intent(PinballShooterActivity.this, HighScoreActivity.class));         
        }     
    });

    Button gameBtn = (Button) findViewById(R.id.startgame); 
    gameBtn.setOnClickListener(new View.OnClickListener() {


         public void onClick(View v) { 
        // TODO Auto-generated method stub             
        startActivity(new Intent(PinballShooterActivity.this, GameActivity.class));         
        }     
    });
}

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

and here is the android manifest

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity 
            android:name=".SoundActivity" />
        <activity 
            android:name=".HighScoreActivity" />
        <activity 
            android:name=".GameActivity" 
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.pinball.shooter.GAMEACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
            </activity>
        <activity
            android:name=".Splash"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".PinballShooterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action           android:name="com.pinball.shooter.faiz.PINBALLSHOOTERACTIVITY"/>

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

Answers


As I pointed out in my comment above, the problem lies not in SoundActivity, but in MainActivity: when MainActivity is paused after launching SoundActivity, it finishes due to finish() in onPause():

public class PinballShooterActivity extends Activity {

    ...

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

    ...

}

Simply remove finish() and your problem will be resolved.


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

You have finish() in your onPause(). Everytime your SoundActivity is called, MainActivity goes to the background and its onPause() is called, which in turn calls your finish() and finishes the Activity.


Need Your Help