0

I have an app that uses intent-filters in the AndroidManifest.xml file to start an Activity when the Home button is pressed. Thing is, if my app is on that time in a different activity, the LoginActivity is started again and the user has to log in again. I want the Home button to do nothing if my app is on top.

How could I do that?

    <activity 
        android:name="com.example.LoginActivity"
        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.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>            
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

3 Answers3

0

Is not advisable to change the Home button functionality, because doing so it would be a negative user experience. Instead, you could store into a variable a boolean if the user is logged in, and if true, jump directly on to the main activity.

I don't know what is your launch activity, but if it is the login activity, you could check on the onStart method what I said, and then start the other activity.

programmer23
  • 533
  • 4
  • 15
0

You shoud to check by your own trigger (static variable?) that your app is working and user logged in early, then start another activity and finish current

OR

try this android:launchMode="singleTop" in your manifest to the activities.

Kirill Shalnov
  • 2,216
  • 1
  • 19
  • 21
0

There are three ways to do this:

  1. Works between 2.3 and 4.0:

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_HOME)) {
            // do nothing                     
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    
  2. Also works between 2.3 and 4.0:

    @Override public void onAttachedToWindow() { 
        this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
        super.onAttachedToWindow(); 
    }
    
  3. This involves overriding the method onUserLeaveHint():

    @Override
    protected void onUserLeaveHint() {
        Intent intent = getIntent();
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        ((AlarmManager) this.getSystemService(ALARM)).set(1,
                System.currentTimeMillis(),
                PendingIntent.getActivity(this, 0, intent, 0));
        }
        super.onUserLeaveHint();
    }
    
Yash Sampat
  • 30,051
  • 12
  • 94
  • 120