简体   繁体   中英

Start with different activity on first launch of android app

Is there a way to launch a different activity on startup once only? If I instantly launch my setup activity from my main activity, there is a 1 second pause with a white screen.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent myIntent = new Intent(this, home2.class);
        this.startActivity(myIntent);
        finish();
        ...

    }

That can be done in several ways. One of them is usage of the shared preferences where will be stored data about accessed activity (for example intro activity).

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
boolean isAccessed = prefs.getBoolean(getString(R.string.is_accessed), false);
if(!isAccessed) {
    SharedPreferences.Editor edit = prefs.edit();
    edit.putBoolean(getString(R.string.is_accessed), Boolean.TRUE);
    edit.commit();
    showIntroActivity();
} else {
    startReqularActivity();
}

Also there is more ways to accomplish that request (for example -> storing accessing state in db, or properties file, or storing on the cloud if application have to be controlled from some back office). IMO this is the best way for achieving that functionality - and of course the simplest.

This is only idea (which is fully functional) and you can adapt it for your needs.

There is a solution described in the book Android Programming Pushing the Limits by Erik Hellman. You need to do the following:

  1. Create your activities (well, let's just say they are main and setup activities) and add them to your manifest like that:

     <activity android:name=”.SetupActivity” android:label=”@string/app_name_setup” android:icon=”@drawable/app_setup_icon” android:enabled=”true”> <intent-filter> <action android:name=”android.intent.action.MAIN”/> <category android:name=”android.intent.category.LAUNCHER”/> </intent-filter> </activity> <activity android:name=”.MainActivity” android:label=”@string/app_name” android:icon=”@string/app_icon” android:enabled=”false”> <intent-filter> <action android:name=”android.intent.action.MAIN”/> <category android:name=”android.intent.category.LAUNCHER”/> </intent-filter> </activity> 

  2. When Setup Activity is shown, disable it and enable Main Activity programmatically:

     PackageManager packageManager = getPackageManager(); packageManager .setComponentEnabledSetting(new ComponentName(this, MainActivity.class), PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP); packageManager .setComponentEnabledSetting(new ComponentName(this, SetupActivity.class), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP); 

I haven't tried this yet, but it still should work just fine.

I think that your original code is very close to what you want. There are a couple of things that you need to do to make it work visually, though.

First, only call setContentView if you actually intend to show the MainActivity. Second, make sure that the SetupActivity loads quickly to avoid the delay. That means making sure that you don't do anything time-consuming in onCreate.

So, then, you can change your code for MainActivity to:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if(isFirstTime()) {
           startActivity(new Intent(this, SetupActivity.class));
           finish();
        } else {
           setContentView(R.layout.activity_main);
           ... // anything else that you need to do to initialize MainActivity
        }
    }

尝试将这段代码放在setContentView()之前

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