How can I make an Activity appear only once for the user?

3

My plan is as follows: In my project I have 2 Activitys; These two activities are only welcome to the user, so I would need them to appear only the first time the user started the application.

I started to study Shared Preferences, but the materials I find out there are just about how to do Login / Password for the user. I did not find any demonstration of how to perform my problem. Can I use Shared Preferences? Which way?

    
asked by anonymous 20.11.2014 / 22:01

1 answer

2

No different from login systems as you found or as suggested. Assuming you have a BoasVindasActivity and a MainActivity (the latter defined as the primary in your manifest file), there is something like MainActivity :

SharedPreferences sharedPrefs = getApplicationContext().getSharedPreferences(NOME_PREFS, Context.MODE_PRIVATE);

if (!sharedPrefs.getBoolean("primeiroAcesso", false)) {
    Intent intent = new Intent(this, BoasVindasActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    startActivity(intent);
    finish();

    return;
}

That is, if you entered the main and do not have primeiroAcesso saved in user preferences, you go to the welcome screen. Otherwise, in your BoasVindasActivity you save this option at some point to open the main:

SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putBoolean("primeiroAcesso", true);
editor.commit();

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

The same check in the beginning you can also do in the BoasVindasActivity , to avoid opening this screen if it is already saved.

    
21.11.2014 / 12:59