Soham Pandya
Soham Pandya

Reputation: 396

restart my app activity on home button pressed

I want to disable all 3 button back button task and home button only for 1 activity , i found a way for back and task button its working.

But I am facing problem for home button disable .. if disabling is little complicated , just restart my app activity when user pressed home button , will also worked for me !

Any suggestion or help how can i do it ?

following is my code

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode==KeyEvent.KEYCODE_HOME)
        {

            return false;
        }
        else if (keyCode==KeyEvent.KEYCODE_BACK)
        {

        }
        if (keyCode==KeyEvent.KEYCODE_TAB)
        {

        }

        else {
            return super.onKeyUp(keyCode, event);
        }
        return  false;
    }

Upvotes: 1

Views: 2072

Answers (4)

Shruti
Shruti

Reputation: 311

if someone looking for answer following might help, Just add this one line in manifest activity tag

 android:launchMode="singleTop"

i.e.

<activity
        android:name=".Ui.MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Upvotes: 1

Omar Aflak
Omar Aflak

Reputation: 2962

Unfortunately, the new version of Android prevent us to override the Home button... sorry for that!

However, you can do something when the button is pressed. You need to override onPause()

EDIT: I tried this, and it worked (even though it's really ugly...):

@Override
public void onPause() {
    super.onPause();

    Log.d(TAG, "Pause");
    Intent i = new Intent(MainActivity.this, MainActivity.class);
    startActivity(i);
}

Upvotes: 0

cheko506
cheko506

Reputation: 368

You can try using this line at the activity in the manifest.

<category android:name="android.intent.category.HOME"/>

Using this, when user press the home button the system will ask you if you want to open for one time or always, select always and when user press home button will open the activity with the categoty home declared

Why is category HOME required?

Upvotes: 0

kimchibooty
kimchibooty

Reputation: 339

I think you need to be overriding onKeyDown

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_HOME)) {
        //stuff here!                 
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: -1

Related Questions