Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4030868
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:31:34+00:00 2026-05-20T11:31:34+00:00

I want to kill an Activity when the user presses the Home button. For

  • 0

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) {

            }
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:31:35+00:00Added an answer on May 20, 2026 at 11:31 am

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the code: public void startAlarm(Context context) { Intent intent = new Intent(context,
I want to modify the following as it doesn't seem to kill processes -
Debugging experience http://www.dmhermitage.org/wtfborders.png This is making me want to kill myself. I have some
I want to kill a process running on the machine using taskkill if they're
I want to kill an executable of a game named KnightOnline and deny user
I want to kill a whole process tree. What is the best way to
I want to assign the decimal variable "trans" to the double variable "this.Opacity". decimal
I want to create a Java application bundle for Mac without using Mac. According
I want to kill a process programmatically in vista/windows 7 (I'm not sure if
I want to be able to log when a user ends their session on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.