0

I am working on an Android app.

There is a web admin panel where I can forced an app user to logout from the app. That feature is working fine. The current app user is forced to logout and gets a Toast message reporting him that the administrator has canceled his account. The login screen activity is shown.

My issue is that I want to terminate the activity that is getting the order to force the logout, but with my current code the activity is not terminated. The login screen is shown but in the background the first activity is running, then it shows the Toast again and again.

This the current code for the logout function:

 private void logoutUser() {


        SharedPreferences prefs2 =
                getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor2 = prefs2.edit();
        editor2.putString("LOGEADO", "NO");
        editor2.apply();

        // Launching the login activity
        Intent intent = new Intent(this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        this.finish(); // Call once you redirect to another activity

    }
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • (untested) finish first using `finishAffinity()` **before** navigating to the LoginActivity. This means you have to remove your `this.finish` call – Zun Jul 11 '19 at 13:35
  • If you find my answer is helpful then please vote it – Jasurbek Jul 01 '20 at 17:10

1 Answers1

0

Add FLAG_ACTIVITY_NEW_TASK to your intent like

Second thing use addFlags not setFlags

private void logoutUser() {


        SharedPreferences prefs2 =
                getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);

        SharedPreferences.Editor editor2 = prefs2.edit();
        editor2.putString("LOGEADO", "NO");
        editor2.apply();

        // Launching the login activity
        Intent intent = new Intent(this, LoginActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
         stopActivity(); // Call once you redirect to another activity

    }

    private void stopActivity() {
       finish()
    }

Another solution

you can set android:excludeFromRecents="true" and android:noHistory="true" in your android Manifest file for the activity and no need to finish your activity

Tricky solution

You can take advantage of the activity lifecycle. It is sure onPause is called while navigating between activities, but you onPause could be called when a user leaves your app as well. That is why I declare one boolean for checking

private needFinish = false;

    private void logoutUser() {


            SharedPreferences prefs2 =
                    getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);

            SharedPreferences.Editor editor2 = prefs2.edit();
            editor2.putString("LOGEADO", "NO");
            editor2.apply();

            // Launching the login activity
            Intent intent = new Intent(this, LoginActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
             needFinish = true; // Call once you redirect to another activity

        }
private void stopActivity() {
           finish()
        }


@Override
public void onPause() {
    super.onPause();
    if(needFinish) stopActivity();

}
Jasurbek
  • 2,946
  • 3
  • 20
  • 37