C Sharper
C Sharper

Reputation: 8626

App not getting minimized on home button click

I want to write some code on Home button clicked by user in my app.

I written following code:

 @Override
        public void onAttachedToWindow() {
            super.onAttachedToWindow();
            this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
        }

     @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {     

            if(keyCode == KeyEvent.KEYCODE_HOME)
            {
               //The Code Want to Perform. 
                Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();

            }
            return true;
        }

This code gives me Toast message, but does not minize my app.

Once i remove following code:

  @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
            }

and keep only:

 @Override
            public boolean onKeyDown(int keyCode, KeyEvent event) {     

                if(keyCode == KeyEvent.KEYCODE_HOME)
                {
                   //The Code Want to Perform. 
                    Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();

                }
                return true;
            }

It does not shows me toast message.

Please help me.

I also tried:

Return false

Onpause method

but not worked.

Upvotes: 0

Views: 1028

Answers (3)

matthewrdev
matthewrdev

Reputation: 12180

Returning true in onKeyDown signals that the event has been processed.

Modify your code to return false so that the event is further processed by the OS.

For example:

        public boolean onKeyDown(int keyCode, KeyEvent event) {     

            if(keyCode == KeyEvent.KEYCODE_HOME)
            {
               //The Code Want to Perform. 
                Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();

            }
            return false; // Signals the KeyEvent.KEYCODE_HOME to be processed further.
        }

See the onKeyDown documentation

Upvotes: 2

Andro Selva
Andro Selva

Reputation: 54322

You should have this code,

 @Override
            public void onAttachedToWindow() {
                super.onAttachedToWindow();
                this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);           
            }

in your code. Removing this will not trigger the Home key action.

Now to minimize the app, do the following,

    @Override
                public boolean onKeyDown(int keyCode, KeyEvent event) {     

                    if(keyCode == KeyEvent.KEYCODE_HOME)
                    {
                       //The Code Want to Perform. 
                        Toast.makeText(getApplicationContext(), flag+"In Here", Toast.LENGTH_SHORT).show();
                 Intent gotoHome= new Intent(Intent.ACTION_MAIN);
                 gotoHome.addCategory(Intent.CATEGORY_HOME);
                 gotoHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 startActivity(gotoHome);
                    }
                    return true;
                }

But I think this solution no more works for higher API levels.

Upvotes: 2

Sunkas
Sunkas

Reputation: 9590

Try return false instead so you don't catch the click event but sends it along to the OS as well.

Upvotes: 2

Related Questions