Andorid - Open Activity only once

Asked

Viewed 123 times

1

How do I have an Activity open only the first time the app starts?

2 answers

2


First, conceptually the first time of use of an application, if the control is offline, is done directly by the application itself, that is, you need to do this control yourself.

You can do this in many ways. The simplest is to use Shared Preferences.

For example, before the possible initialization of Activity you can check the existence of a certain value. See:

//obter SharedPreferences
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
//chave de checagem
String isnew = prefs.getString("primeira_vez", null);
if (isnew != null) {
  String name = prefs.getString("name", "Não é a primeira vez");
  int idName = prefs.getInt("idName", 0); //0 is the default value.
} else {
  //... inicialize a atividade
  //... altera o valor de "primeira_vez" para "usado"
}

0

You can create in Main, a flag on SharedPreferences, and check it every time the app starts. If the flag has false, opens the activity desired and already changes to true.

I didn’t find here a project I had done, but I found a HERE.

Example:

public class Splash extends Activity {

    SharedPreferences sharedpreferences;
    boolean splash;

    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);

        sharedpreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        splash = sharedpreferences.getBoolean("Splash", false);
        if (splash == true) {
            Intent mainIntent = new Intent(Splash.this, MainActivity.class);
            startActivity(mainIntent);
        }
        else
        {
            setContentView(R.layout.splash);

            //animation
            ImageView imageView = (ImageView) findViewById(R.id.imageView);
            Animation alpha = AnimationUtils.loadAnimation(getApplicationContext(),
                    R.anim.fade_in);

            imageView.startAnimation(alpha);

            alpha.setAnimationListener((new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                    // TODO Auto-generated method stub
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    // TODO Start your activity here.
                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putBoolean("Splash", true);
                    Intent mainIntent = new Intent(Splash.this, MainActivity.class);
                    Splash.this.startActivity(mainIntent);
                    Splash.this.finish();
                }
            }));
        }
    }
}

Browser other questions tagged

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