Shift when closing application

1
I have an exit button that causes the user to log off from the system, which works like this:

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 it when I close the App it shuts, but not when I minimize and leave it open in the background, only when it actually is finalized.

    
asked by anonymous 15.01.2018 / 20:29

1 answer

2

I had recommended that you could use the onDestroy method, 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 do not need to clear the data when the user exits the app, but rather when it comes in.

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

  

SplahActivity

We will not use any layout in our SplashActivity because it is not necessary. We do not want the user to waste a lot of time on it, it's going to be something quick, we'll just show the app icon and that's it.

<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 windowBackground attribute, as it will position the icon for our application on SplashScreen. You can swap for a Drawable too if you want to make a custom image or leave it with no background image.

Do not forget to set this class in Manifest as 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 that inherits from Application and then clean up 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();
    }
}

Do not forget to point to the Manifest in the Application tag the name for your MyApp class.

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

As mentioned, the onDestroy method is a bit of a bad thing to put because it is not always called .

    
15.01.2018 / 20:48