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) {
}
}
}
I think you need to give a bit more information about your motivation here: why do you want to kill your activity on Home presses instead of letting the system manage it? What are you trying to accomplish that isn't happening on its own? If it's just a matter of not wanting users task-switching back into a specific activity, check out the various flags you can use for the task stack.
EDIT: In this case, you're probably causing the problem with your catch-all exception handling. Try moving these lines into the finally
clause, instead of in the try
, and have your Exception handler at least log the exception it's catching (Log.e("tag", "Exception caught; ignoring:", e)
). Catch-alls are generally bad programming practice, especially if you're not at least logging the exception so you can see what goes wrong.
Intent intent = new Intent(getApplicationContext(), ScreenTwo.class);
startActivity(intent);