Make an Acitivity on Android appear only once

Asked

Viewed 771 times

0

Well, my question is: How do I make an Activity appear only once, and then when the user opens the app again this screen does not appear, only the others. I’ll explain how my project is to be clearer

My project is very simple. There are 3 screens, the first of Registration, the second that is the "main" and the last one a recovery screen.

In the first screen the user registers an email and a password. On Monday he uses the registered password to get off the phone. And at last like I said, it’s a password recovery screen.

And I wanted that after the user made the registration on the first screen and was redirected to the second screen, the first screen no longer appear, I just want it to appear 1 time for the user to register and then no more.

  • Read: http://answall.com/questions/51093/realizar-uma-a%C3%A7%C3%A3o-only-when-app-is-started-first-time-ap%C3%B3s-a-ins/51099#51099

  • The questions are different, there is when the person downloaded the app, and when she download an att does not open a screen, there it moves version of the app my no, I want the screen open once and then no more, even with att or not.

  • It turns out that you will use the same function for your case. If the user successfully registers, run the code and save in Sharedpreferences.

  • The questions are different, but the application is exactly the same. :)

  • I agree with the @Ramaral signage because what the comrade wants can be easily achieved with a splashscreen only that instead of using a timer use an onclick event.

  • @array is just that. A(one possible) approach is that, both in this question and the other. Just see the answers that were given here.

  • @ramaral yes, the possible alternatives would be to save in a file . ini or in a Sqlite database, but I believe that SharedPreferences be the best method.

Show 2 more comments

2 answers

2


You can do it two ways...
One is using Sharedpreferences to save a "little tag" and every time you open the app you will have to check if it is there and, if it is, do not open Activity.
The other way is to save a tag to sqlite and do the same check, but not recommend it. sharedPreferences is like this:

    private void setSharedPrefs(Context contexto,
                                       String nomeProjeto,
                                       String chave,
                                       String valor) {
        SharedPreferences sharedPreferences;
        sharedPreferences = contexto.getSharedPreferences(nomeProjeto, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(chave, valor);
        editor.apply();
    }

    private String getSharedPrefs(Context contexto,
                                         String nomeProjeto,
                                         String chave) {
        SharedPreferences sharedPreferences;
        sharedPreferences = contexto.getSharedPreferences(nomeProjeto, Context.MODE_PRIVATE);
        return sharedPreferences.getString(chave, null);
    }

Use:

        setSharedPrefs(this,"meuProjeto", "meuValor", "HAHAHAHAHA");
        String risada = getSharedPrefs(this,"meuProjeto", "meuValor");
        Log.e("funcionou", risada);

In your case, put type like this in the oncreate of the first Activity:

    if(getSharedPrefs(this,"meuProjeto", "meuValor") != null){
       // Se tem algum valor guardado, chama o intent pra trocar de tela
    } else {
        // primeira vez que abriu, então seta o layout normal...
        setContentView(R.id.minhaPrimeiraActivity);
    }

1

In your case the simplest way is to save in Sharedpreferences and check if this screen has already been opened for the user.

  • I tried to use Sharedpreferences, only I think I did the wrong code because it didn’t change anything, I did all the procedures close the app, and when I opened for the second time the screen I didn’t want it to open, it opened even with the code.

  • He testified to see why this behavior?

Browser other questions tagged

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