Showing the setup screen only on first launch in android

后端 未结 2 587
旧巷少年郎
旧巷少年郎 2021-01-03 09:18

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 la

相关标签:
2条回答
  • 2021-01-03 09:55

    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
    
    0 讨论(0)
  • 2021-01-03 10:00

    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();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题