sharedpreferences in the first Activity, show only once?

Asked

Viewed 668 times

1

I’m developing an app in which you will have the first Activity greeting, and I want to show it only once, only when the app first opens.

In this case, I created a Activity calling a layout containing the textview and button, as soon as the user clicks on the button, will open the Activity main app, and then do not want to Activity greeting be shown more.

My layout (summarized):

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Saudação Aqui" />

<Button 
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="sendMessage"
android:text="Ok, entrar para Activity principal e não mostrar mais essa tela" />

Greeting activity:

Button button;

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     button=(Button)findViewById(R.id.button1); 

    button.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 

    Intent i = new Intent(getApplicationContext(),Activity Principal.class);
    startActivity(i); 
} 
}); 
}

Thank you very much!

  • 1

    I don’t understand your doubt!! You have already implemented something to save on Sharedpreferences ?

  • 1

    You can find something similar here in this answer.

  • @Thiagoluizdomacoski It is that I am beginner friend, I am right at the beginning of the programming world. What I wanted to manipulate, was this Activity (greeting) to show only once. I don’t know exactly how to "mount" (implement) with sharedpreferences. In this case, you could give me an example of how to do this manipulation?

  • @Paulorodrigues Ok friend, thank you very much. I will take a look ;)

1 answer

3

First you implement a class to manage Sharedpreferences (by organization).

public class PreferencesManager {
    public static final String ENTERING_FIRST_TIME = "EnteringFirstTime";

    public static void storeInt(Context context, String key, int value) {
        SharedPreferences prefs = context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static int getInt(Context context, String key, int defaultValue){
        return context.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE).getInt(key, defaultValue);
    }
}

So when your app starts, you can check this way:

if(PreferencesManager.getInt(view, PreferencesManager.ENTERING_FIRST_TIME, 1) == 1){
    //Salva informação de que o usuário já entrou no app a primeira vez
    PreferencesManager.storeInt(view, PreferencesManager.ENTERING_FIRST_TIME, 0);
    //Exibe saudação
}
else{
    //Esconde saudação
}

The ideal is to read and write information in Sharedpreferences outside the UI thread, to prevent your application from being destroyed by ANR (Application Not Responding). ANR’s happen when your application processes something longer than allowed in the Main Thread (five seconds).

It may seem impossible for such a simple task to take more than five seconds.

You can prevent this by calling the second code block within a Asynctask.

  • Very good friend, thank you very much for the answer. I was thinking, in this case I could also put a simple dialog (ex: same as those terms of the app with button: Li, I accept, I do not accept) to be shown only once, what do you think? It would be simpler, right? And I believe it would prevent my application from being destroyed by ANR.

  • and what is the criteria to display or not the dialog? No use running away from Asynctask, it will chase you but it’s easy

  • It is that in this case friend, I would put the welcome dialog inside the Main Activity itself and just call sharedpreferences to it to appear only once. That logic is right?

  • It will work, only it is not good practice to do it synchronous... has a very small chance of giving error

Browser other questions tagged

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