Parthi
Parthi

Reputation: 658

Reload the same activity when press Home

I have an application. I tried to disable home button. Many people said that it doesn't possible to do in android 4.0 and above. So i decided to reload the same activity when press home button. I followed the below code.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
    if (keyCode == KeyEvent.KEYCODE_HOME) {
        System.out.println("==============================");
        Intent i = new Intent(getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(
                        getBaseContext().getPackageName()));
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(i);
        return true;
    }
    return false;
}

I can't get any response when press home button. Can you tell me what's my wrong?

Upvotes: 0

Views: 373

Answers (3)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

This key cannot be intercepted, so KEYCODE_HOME won't be send to you.

Upvotes: 1

nawaab saab
nawaab saab

Reputation: 1902

This is a refresh button method, but it works well in my application. in finish() you kill the instances

refresh = (Button)findViewById(R.id.refresh);
        refresh.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                onRestart();

            }
        });
    @Override
    protected void onRestart() {

        // TODO Auto-generated method stub
        super.onRestart();
        Intent i = new Intent(lala.this, lala.class);  //your class
        startActivity(i);
        finish();

    }

Upvotes: 0

amalBit
amalBit

Reputation: 12181

Its impossible to override the home button.

public static final int KEYCODE_HOME

Added in API level 1
Key code constant: Home key. This key is handled by the framework and is never delivered to applications.

Source: http://developer.android.com/reference/android/view/KeyEvent.html

Upvotes: 1

Related Questions