简体   繁体   中英

Disable android home button without onCreate being called multiple times

Before beginning I want to state that I know disabling the home button is typically not a good idea (or generally possible). For the application I am working on though it is 100% necessary. This app will not be deployed out in the wild and moreover will be the primary/"only" app in use on the device (by design). Without giving more information I hope that this will be sufficient to address the "this is a really bad idea", "why would you want to do that", "you should use a different technology", etc points/questions.

That said, disabling the home button is not something that you can really do in android but I can somewhat emulate the desired behavior with the following code

private TextView mTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());
    mTextView = binding.textView;
    mTextView.setText("Start");
}

@Override
public void onPause()
{
    super.onPause();
    mTextView.setText("pause called");
}

and xml code (with app/package name removed)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="....">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.REORDER_TASKS" />
    <uses-feature android:name="android.hardware.type.watch" />


    <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
       android:supportsRtl="true"
       android:theme="@android:style/Theme.DeviceDefault">
        <uses-library
            android:name="com.google.android.wearable"
            android:required="true" />

        <!--
              Set to true if your app is Standalone, that is, it does not require the handheld
               app to run.
        -->
        <meta-data
           android:name="com.google.android.wearable.standalone"
           android:value="true" />

       <activity
           android:name=".MainActivity"
           android:exported="true"
           android:label="@string/app_name">
          <intent-filter>
              <action android:name="android.intent.action.MAIN" />
              <category android:name="android.intent.category.HOME" />
              <category android:name="android.intent.category.LAUNCHER" />
          </intent-filter>
        </activity>
    </application>
</manifest>

This code works completely fine with one exception, the first time the home button is pushed onCreate is called a second time after the app is created. From a UI perspective I will see the mTextView show "Start" until the home button is pressed at which time it goes to "pause" (because onPause was called) followed immediately by "Start again (indicating that onCreate was called a second time). Further pressing the home button does nothing (as desired).

Is there a way to prevent onCreate being called a second time or another way to emulate disabling the home button that won't result in the UI being reloaded/changed (I could save the state and then load the state after the home button is pressed but the UI will still be reloaded; the screen will go black for a second or two which I don't want)?

I don't think it makes that much of a difference, but I am using the API 30: Android 11.0 (R) sdk for deployment on a Samsung Galaxy Watch4 classic. Also as a final point, I don't want to root the device (which would solve this issue).

"you should use a different technology"

Probably yes. If this were a traditional form factor, you might look into things like kiosk mode or custom firmware. Or, in your case, since this is Wear OS, perhaps you could implement a watchface .

Is there a way to prevent onCreate being called a second time

Presumably, a second instance of your activity is being created. If so, and assuming that this is a single-activity app (as your manifest indicates), try android:launchMode="singleInstance" , or possibly singleTask .

I am using the API 30: Android 11.0 (R) sdk for deployment on a Samsung Galaxy Watch4 classic

I would not assume that your app will be able to take over HOME on Wear OS indefinitely. Frankly, I am surprised that it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM