0

I have a certain activity that begins when I tap my smart watch's screen. There is a timer and bunch of stuff that happens, but the process is crucial, so I am handling certain cases or things that might happen that would disturb the flow of things.

So basically, I want to prevent the home button of my watch to exit the app and go to the homescreen while my timer is running. I keep looking this up and most people say to override the onBackPressed method. But this was for the back button, and I I realized the button is a home button not a back button.

 frameLayout.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN)
            {
                clicked = clicked + 1;
                if (clicked == 2)
                {
                    Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
                    startTimer();
                }
                else if (clicked >= 4)
                {
                    Toast.makeText(toolandmode.this, "Clicks:" + clicked, Toast.LENGTH_SHORT).show();
                    AlertMessage();
                }
            }
            return true;
        }
    });

this is the main method I use

3 Answers3

1

Just override the onBackPressed function.

@Override
public void onBackPressed ()
{
  //Control the flow 
 }
The Codepreneur
  • 236
  • 3
  • 12
  • I did but it's not working for some reason, I mean isn't it the same in Wearable devices? Do I need to call the function inside my timer method or inside the if statement in the code sample I presented? – Mohamed El Kayal Feb 08 '21 at 15:12
0

Use third part library in case if it solves your problem. Here is the link: https://github.com/shaobin0604/Android-HomeKey-Locker

Danish
  • 676
  • 5
  • 10
  • I think I found the problem, the button is the home button, the watch goes back using a swipe left gesture. So whtat is the equivalent of what I asked for but for the home button? – Mohamed El Kayal Feb 08 '21 at 15:58
  • not sure how this works, what's the logic behind it? – Mohamed El Kayal Feb 08 '21 at 16:18
  • It's actually pretty simple I want the home button disabled if the startTimer() method is called so basically when the if statement above is fulfilled I want the home button disabled. – Mohamed El Kayal Feb 08 '21 at 18:20
  • Sorry i havent read the question properly try the updated answer. – Danish Feb 08 '21 at 18:31
  • nope, is it perhaps a little different with wearables? – Mohamed El Kayal Feb 08 '21 at 18:37
  • Actually the thing is you cant do that because home button is a system button and android do not allow us to do that.The above would have worked but that would require you to set your home button to open your app again and again whenever the user presses it.Read this:https://stackoverflow.com/a/29152388/14979180 – Danish Feb 08 '21 at 19:07
  • what about the back button isn't it a system button as well? I've seen people disabling that. I mean it's not like I want to permanently disable it, just while the timer is running. – Mohamed El Kayal Feb 08 '21 at 19:11
  • ya u can do it by using third party library to disable your home button but it will disable back button too.If you want i can share the link of that library. – Danish Feb 08 '21 at 19:14
  • I think I might just settle for disabling the left swipe gesture since this is more important, and as for the home button, I think it might not be a priority for now. Okay give me the link just in case. – Mohamed El Kayal Feb 08 '21 at 19:17
  • https://github.com/shaobin0604/Android-HomeKey-Locker – Danish Feb 08 '21 at 19:18
0

The general consensus is that you can't override the home button behavior on a Wear OS device, just like you can't override the home button on an Android phone. This is by design to prevent developers from preventing the user to leave an application. Even if there is a hacky way to do it, this is not officially supported and may stop working at any time in future OS versions. I highly suggest not doing it since it goes against the basic navigation model of the device.

More details, and some workarounds for common use cases where people think they need to override the home button can be found in this blog post.

TofferJ
  • 4,678
  • 1
  • 37
  • 49