Logout when closing app

Asked

Viewed 444 times

1

Guys, I have an app that runs login, for this I use sharedPreferences(), I have a quit button that causes the user to depress the system, which works this way:

SharedPreferences pref = getSharedPreferences("info",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.clear();
editor.commit();
Log.d("ESTADO_APP", "Deslogado");
finish();  

And it works perfectly, I would like when I close the App it will deploy, but not when minimize and leave open in the background, only when it is actually finalized.

  • Try to put your logout logic into ondestroy? If it doesn’t work here is the answer that suggests you create a service to notify when your tasks are destroyed: https://stackoverflow.com/questions/41744933/android-ondestroy-isnt-called-when-i-close-the-app-from-the-recent-apps-button

1 answer

2


I had recommended that you could use the method onDestroy, which is called when the system needs memory or when the Finish() method is called. But this might not work in some cases. So, the best thing to do is: you don’t need to wipe the data when the user leaves the app, but when they log in.

For this, we make a SplashActivity or a class Application. You can choose any one.

Splahactivity

We will not use any layout in our Splashactivity because it is not necessary. We don’t want the user to waste much time on it, it will be something fast, we will only show the app icon and ready.

<style name="AppTheme.Splash" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:windowBackground">@mipmap/ic_launcher</item>
</style>

In this style tag, focus on the attribute windowBackground, because he is the one who will position the icon of our app on Splashscreen. You can exchange for a Drawable as well, if you want to make a custom image or leave without any background image.

Don’t forget to define this class on Manifest like the Entrypoint of the application.

public class SplashActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle cycle) {
        super.onCreate(cycle);
        clearData();
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    private void clearData() {
        SharedPreferences pref = getSharedPreferences("info",MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.commit();
    }
}


<activity
        android:name="br.com.exemplo.SplashActivity" <!-- muda para o endereço correto -->
        android:theme="@style/AppTheme.Splash">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

Application

You will only need to create a class you inherit from Application and then clear the data.

public class MyApp extends Application {

    @Override
    public void onCreate() {
       clearData();
       startActivity(new Intent(this, MainActivity.class));
       finish();
    }

    private void clearData() {
        SharedPreferences pref = getSharedPreferences("info",MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.clear();
        editor.commit();
    }
}

Don’t forget to point out in the Manifest in the Application tag the name for your Myapp class.

<Application
 .....
    android:name=".package.MyApp" />

As it was said, in the onDestroy method it gets kind of bad to put because it is not always called.

  • Where do I create the style tag? in the Layout folder?

  • Oops, excuse me for not telling you this. You can copy and paste the code style in the archive styles.xml which gets into res/values

  • Brenddon, I edited the answer and added more details.

  • Thanks man, with Splahactivity, if I press the back button in Activity after login it does not close the app but calls the splash then scrolls, and with Aplication works normal, only when logging in and pressing the back button then it goes back to the login page. My app works like this Splash > Login > Home

  • I understood why, When I use Application, Entrypoint is on Main(Login page), then it opens twice, because I call it also on Myapp, and Entrypoint is required to have in some Activity, then I decided to redirect to Activity after login and doing the treatment of Sharedpreferences there, you helped mto man!

  • @Brenddonanjos, Entrypoint should be in Splashactivity anyway, because then you send it to the Login screen or to Main. But the app entrypoint (Auncher and tals) must be in Splashactivity itself.

  • I say this because it is a lighter Activity, has no layout and will load much faster than the others. It will only direct the user to the next screen. You can close it with "Finish()".

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.