I want to kill an Activity when the user presses the Home button. For this I'm using the following code:
public void onPause() {
super.onPause();
this.finish();
}
It works fine. But instead of Home if the user presses the Back button it also kills the activity. I want the back button to perform as usual i.e it should take the user to the previous activity. Any thoughts?
The following is the code of my Activity class:
public class HomeScreen extends Activity {
/** Called when the activity is first created. */
private Button btn_play;
private MediaPlayer mp = new MediaPlayer();
private static int AUDIO_NO = 1;
public static String isVideoSelected = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
btn_play = (Button)findViewById(R.id.btn_play);
btn_play.setOnClickListener(btn_listener);
if(isVideoSelected!="") isVideoSelected="";
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
play_audio(AUDIO_NO);
}
}, 1000);
} catch(Exception e) {
}
}
private void play_audio(int slno) {
try {
if(slno == 1) mp = MediaPlayer.create(getBaseContext(), R.raw.audio_1);
else if(slno == 2) mp = MediaPlayer.create(getBaseContext(), R.raw.audio_2);
mp.setLooping(false);
mp.setOnCompletionListener(audio_listener);
mp.start();
} catch(Exception e) {
// do nothing
}
}
private MediaPlayer.OnCompletionListener audio_listener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
try{
mp.release();
if(AUDIO_NO == 1) {
play_audio(2);
AUDIO_NO++;
}
} catch(Exception e) {
}
}
};
private View.OnClickListener btn_listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try{
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
Intent intent = new Intent(getApplicationContext(), ScreenTwo.class);
startActivity(intent);
} catch(Exception e) {
} finally {
}
}
};
public void onUserLeaveHint() {
super.onUserLeaveHint();
try{
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
btn_play = null;
} catch(Exception e) {
}
}
@Override
public void onDestroy() {
super.onDestroy();
try{
if(mp.isPlaying()) {
mp.stop();
mp.release();
}
btn_play = null;
} catch(Exception e) {
}
}
}