How to kill an action when you click the "Home" button?

I want to kill an action when the user clicks the Home button. For this, I use the following code:

public void onPause() {
    super.onPause();
    this.finish();
}

It works great. But instead of Home, if the user clicks the back button, he also kills the activity. I want the back button to execute as usual, so it should transfer the user to the previous operation. Any thoughts?

Below is the code for 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) {

            }
        }
    }
+3
source share
2 answers

, : , ? , ? , , .

: , , catch. finally, try, Exception, , , (Log.e("tag", "Exception caught; ignoring:", e)). Catch-alls - , , , , , , , .

        Intent intent = new Intent(getApplicationContext(), ScreenTwo.class);
        startActivity(intent);
+2

onPause() , , finish(). , "", , .

0

All Articles