The screen for the first screen of preferences and related questions is displayed.

I have an application with two actions, preference and main action. I need the preferences screen to be displayed the first time the application is launched so that the user can perform some configuration. I checked the answers on this topic and they do not seem very clear, but I understand that this has to do with checking that the sharedpreference file is empty.

Can someone please give me a code to figure this out and what activity would I put the code in? I am also still at the development stage, so I already have my settings, how can I cancel this?

Thanks at Advance

+7
source share
4 answers

I assume that you start the emulator when you start the emulator, you have the option to โ€œerase saved dataโ€ when you start it, so it will be like how you started it, as if you just started the application. In addition, you can enter the settings โ†’ Applications โ†’ You application โ†’ Wipe data.

As for your encoding solution, at the moment I donโ€™t have anything convenient, but what you need to do is start your main activity, start a procedure / function to check if the sharedpreference file is empty, and if it starts working with preference, otherwise follow the main action. Alternatively, instead of checking that the file is empty, you can see if the value you are looking for for input by the user (e.g. UserID) is null or not. If this value is not null, it means that the application can continue.

+2
source

1) When your main action begins, check the boolean value with the default setting of false. If this is a lie, start your preference activity, if it is true, then you know that you saved it to be true!

SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE); boolean haveWeShownPreferences = prefs.getBoolean("HaveShownPrefs", false); if (!haveWeShownPreferences) { // launch the preferences activity } else { // we have already shown the preferences activity before } 

2) In your activity preferences, the same logical preferences with the value true in onCreate

 SharedPreferences prefs = getSharedPreferences("MyPreferences", Context.MODE_PRIVATE); SharedPreferences.Editor ed = prefs.edit(); ed.putBoolean("HaveShownPrefs", true); ed.commit(); 
+14
source

I sorted this with this piece of code in my main action

 if (prefs.getString("edittextpref", null) == null) { startActivity(new Intent(this, Preferences.class)); return; } } 

It just checks if one of your values โ€‹โ€‹is empty, but you need to put it at the beginning of onCreate, or when you return to the main page, it will be empty.

+1
source

I am doing something like this. And his work is for me.

 String path = "//data//data//"+this.getPackageName()+"//shared_prefs//feedbackpref.xml"; boolean exists = (new File(path)).exists(); if (exists) { introWindowNavigate=false; } 
0
source

All Articles