简体   繁体   中英

Showing the setup screen only on first launch in android

I am making an android application but i can't figure out how i can make the setup screen show up only the first time. This is how the application is going to work: User launches the application after installation and is being shown the welcome/setup screen. And once the user is done with the setup, the setup screens will never appear again unless the user reinstalls the application.

How can i make this happen??? Please help and thanks SO much in advance!

Use SharedPreferences to test whether its the first start or not.

Note: The below code was not tested.

In your onCreate (or whereever you want to do things depending on first start or not), add

// here goes standard code 

SharedPreferences pref = getSharedPreferences("mypref", MODE_PRIVATE);

if(pref.getBoolean("firststart", true)){
   // update sharedpreference - another start wont be the first
   SharedPreferences.Editor editor = pref.edit();
   editor.putBoolean("firststart", false);
   editor.commit(); // apply changes

   // first start, show your dialog | first-run code goes here
}

// here goes standard code

Make one helper activity. This will be your launcher activity.It will not contain any layouts, It will just check for first fresh run of an app. If It will first run, then setup activity will be started otherwise MainActivity will be start.

public class HelperActivity extends Activity {

    SharedPreferences prefs = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Perhaps set content view here

        prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (prefs.getBoolean("firstrun", true)) {
            // Do first run stuff here then set 'firstrun' as false
            //strat  DataActivity beacuase its your app first run
            // using the following line to edit/commit prefs
            prefs.edit().putBoolean("firstrun", false).commit();
            startActivity(new Intent(HelperActivity.ths , SetupActivity.class));
            finish();
        }
        else {
        startActivity(new Intent(HelperActivity.ths , MainActivity.class));
        finish();
        }
    }
}

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