3

I have an activity named MainActivity and another called LoginActivity. The main activity cannot be visible to logged out users. In the onCreate method of MainAcitivity I verifiy if the user is logged out and then I create the LoginActivity.

The problem is that if the user press the back button of the cellphone, the app go back to the MainActivity.

How I prevent this?

The code of the LoginAcitivity creation is this:

Intent i = new Intent(_context, LoginActivity.class);

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);         

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

_context.startActivity(i);
Renan Cunha
  • 189
  • 1
  • 2
  • 9

4 Answers4

6

You can call finish(); just after startActivity(). This will kill MainActivity so the back button will close the app.

3

after: _context.startActivity(i);

put: finish();

wernerH
  • 33
  • 6
2

If you're fine with just disabling the back button in the LogInActivity, you can do so with the following code in the LogInActivity class:

@Override
public void onBackPressed() {
}

Taken from here

Community
  • 1
  • 1
aProperFox
  • 2,114
  • 2
  • 20
  • 21
0

AndroidManifest.xml

<activity
    android:noHistory="true"
    android:name=".activity.MainActivity"
    android:screenOrientation="portrait"
    android:label="@string/title_activity_maps">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

<activity
    android:noHistory="true"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize"
    android:name=".activity.LoginActivity" />

I tried "noHistory" in the file "AndroidManifest.xml". Main- and LoginActivity. Works so far in my app.

  • "Back-Button" terminates the app in Login- and MainActivity