Question

I have an application that uses CATEGORY_HOME so that when you push the home button it keeps you within my app. After the user selects my app as the default home it is set for the whole device.

I was wondering how you disable the home going within my app when you are not in my app when pushing the home button. After doing some research I think I need to use the package manager but I do not know how to implement this.

The activity is defined as home in the Manifest:

<activity
        android:name=".home"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

And here is how I go to the home and how I set it:

Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
Was it helpful?

Solution

Why are you configuring category HOME in your manifest ? You can override home pressing behavior by overriding :

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        Toast.makeText(this, "Home button pressed!", Toast.LENGTH_LONG).show();                     
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Then just do whatever it is you're doing when pressing the button inside the app. Outside the app it will work normally of course.

EDIT

Just checked it, doesn't work on my device, but it seems like if you override onAttachedWindow() and disable HOME, it might do the trick. Just remember to reassign the key using onKeyDown() (just like posted above this edit).

@Override
public void onAttachedToWindow() {  
    getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
    super.onAttachedToWindow();  
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top