An activity that starts only once after a new installation?

I want my application to have activity showing instructions for using the application. However, this β€œinstructions” screen should only be displayed once after installation, how do you do it?

+6
source share
2 answers

You can check that a special flag is set in the SharedPreferences application (let firstRun call firstRun ). If not, this is the first run, so show your activity / popup / all with instructions, and then set firstRun to preference.

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SharedPreferences settings = getSharedPreferences("prefs", 0); boolean firstRun = settings.getBoolean("firstRun", true); if ( firstRun ) { // here run your first-time instructions, for example : startActivityForResult( new Intent(context, InstructionsActivity.class), INSTRUCTIONS_CODE); } } // when your InstructionsActivity ends, do not forget to set the firstRun boolean protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == INSTRUCTIONS_CODE) { SharedPreferences settings = getSharedPreferences("prefs", 0); SharedPreferences.Editor editor = settings.edit(); editor.putBoolean("firstRun", false); editor.commit(); } } 
+15
source

yes, you can fix this problem with SharedPreferences

 SharedPreferences pref; SharedPreferences.Editor editor; pref = getSharedPreferences("firstrun", MODE_PRIVATE); editor = pref.edit(); editor.putString("chkRegi","true"); editor.commit(); 

Then check the line chkRegi ture or false

+1
source

Source: https://habr.com/ru/post/905462/


All Articles