简体   繁体   中英

Where to put welcome activity code

I searched a lot but i did not find my answer. I have developed an android application where on the very first lunch user will be shown a welcome screen made of viewpager. The problem is i don't know which place is the best to put the welcome activity code in my application.

The simplest way it could be that in the main activity at the very fist line even before super.onCreate() , inside onCreate method where i try to get the shared preference value and then evaluate whether it is fist lunch. If it is, then i start the welcome activity as shown below

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        boolean welcome = sharedPreferences.getBoolean(getString(R.string.key_welcome), true);
        if (welcome) {
            // go and start welcoming activity
            Intent intent = new Intent(this, WelcomeSlideActivity.class);
            startActivity(intent);
        }


        super.onCreate();
    }
}

But i found another approach to deal with it. It is Application class. Since Application class is the first one, which runs even before any other codes in my application. So i thought, i would be nice to do it there as shown below

public class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        boolean welcome = sharedPreferences.getBoolean(getString(R.string.key_welcome), true);
        if (welcome) {
            // go and start welcoming activity
            Intent intent = new Intent(this, WelcomeSlideActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }
    }
}

So i am in dilemma, which one would be the best option to choose. And i am even not sure if i am doing it in the right way since there is no such documentation in android developer website or anywhere.

Have a look at how to create splash screens the correct way. https://www.bignerdranch.com/blog/splash-screens-the-right-way/

As for using the Application class - this is primarily used for Application-wide configuration for maintaining a global application state. Hence starting an activity from here does not make much sense as it's purpose has changed into becoming an entry point into the application rather than providing state for the app as a whole.

Furthermore, why not make the WelcomeSlideActivity the first 'launcher' activity? Then in there, you can create the logic of whether to launch the next activity without history or whether to show the current view.

Ideally, you should create a splash screen activity, which determines whether to show the WelcomeSlideActivity Or the MainActivity . The advantage of this is that while he app determines which Activity to launch, the user is presented with a splash screen that informs the user that the app has started

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM