How to create a one-time welcome screen using Android settings?

I would like to create a screen that appears only once after the application starts. After that, it will only display the main screen. The way I did this was to simply check the settings and set the current layout based on the flag. Are there any opportunities for its implementation? Is there a better way?

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Here is the main layout setContentView(R.layout.main); mPrefs = PreferenceManager.getDefaultSharedPreferences(this); // second argument is the default to use if the preference can't be found Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false); if (!welcomeScreenShown) { //Here I set the one-time layout setContentView(R.layout.popup_message); SharedPreferences.Editor editor = mPrefs.edit(); editor.putBoolean(welcomeScreenShownPref, true); editor.commit(); // Very important to save the preference } } 
+2
source share
2 answers

Try using the application version code . Below is an example of the code I used;

  SharedPreferences sharedPreferences = getSharedPreferences("version", 0); int savedVersionCode = sharedPreferences.getInt("VersionCode", 0); int appVershionCode = 0; try { appVershionCode = getPackageManager().getPackageInfo(getPackageName(), 0).versionCode; } catch (NameNotFoundException nnfe) { Log.w(TAG, "$ Exception caz of appVershionCode : " + nnfe); } if(savedVersionCode == appVershionCode){ Log.d(TAG, "$$ savedVersionCode == appVershionCode"); }else{ Log.d(TAG, "$$ savedVersionCode != appVershionCode"); SharedPreferences.Editor sharedPreferencesEditor = sharedPreferences.edit(); sharedPreferencesEditor.putInt("VersionCode", appVershionCode); sharedPreferencesEditor.commit(); Builder alertDialogBuilder = new Builder(this); alertDialogBuilder.setTitle("Version"); alertDialogBuilder.setMessage("This is one time show dialog box "); alertDialogBuilder.setNeutralButton("Close", new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Log.d(TAG, "$$ onClick"); } }); alertDialogBuilder.show(); } 
+6
source

Instead of general preference, you can use the code below, also I used it many times, it will work fine, it shows only once when the application starts for the first time

 public class SplashActivity extends Activity { protected boolean _isActive = true; protected int _splashTime = 3000; //SplashActivity will be visible for 2s final String TAG = "SplashActivity"; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash_activity); //a separate thread to manage splash screen final Thread splashThread = new Thread() { public void run() { try { int wait = 0; while (_isActive && (_splashTime > wait)) { //will show only on the first time sleep(100); if (_isActive) { wait += 100; } } } catch (InterruptedException e) { Log.d(TAG, e.getMessage()); } finally { startActivity(new Intent(SplashActivity.this, MainActivityAbs.class)); finish(); } } }; splashThread.start(); } //if a user clicks on a back btnOrder, do not show splash screen public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { _isActive = false; } return true; } 
+1
source

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


All Articles