Search code examples
androidsplash-screenpreference

Splash Screen start activity depending on preference?


I'm doing some more renovations to my app and I'm wanting to allow the user to select from two different (although very similar) UI's. I already have both different styles set up but I'm still very new to building applications.

Basically here's what I'm wanting to happen:

  • On the first launch show the splash screen I've created with two buttons and instructions on what to do (choose which UI they would like to have) and store their choice somewhere

  • On any launch after they have made their decision send them to the UI they have chosen and not show the splash screen

  • Have an option (somewhere in the options menu) to allow them to change the UI of the application

The only thing that I'm having trouble with is the java for the splash screen. If someone could help me with that, then I SHOULD be able to work up the rest myself.

Thanks in advance!


Solution

  • I have a solution for the splash screen to be appear only at the first time of running the application by using the shared preferences. Try this,

    public class Splash extends Activity {
        private long splashDelay = 1500;
        int counter;
        SharedPreferences app_preferences;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash);
    
            app_preferences = PreferenceManager.getDefaultSharedPreferences(this);
            counter = app_preferences.getInt("counter", 1);
            System.out.println("count is..." + counter);
    
            TimerTask task = new TimerTask() {
                @Override
                public void run() {
                    finish();
                    if (counter == 1) {
                        Intent in = new Intent(Splash.this, Yourclass1.class);
                        startActivity(in);
                    } else {
                        Intent intent = new Intent().setClass(Splash.this, Yourclass2.class);
                        startActivity(hackbookIntent);
                    }
                    SharedPreferences.Editor editor = app_preferences.edit();
                    editor.putInt("counter", +(counter + 1));
                    editor.commit();
    
                }
    
            };
    
            Timer timer = new Timer();
            timer.schedule(task, splashDelay);
        }
    }