Showing the setup screen only on first launch in android

后端 未结 2 609
旧巷少年郎
旧巷少年郎 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 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();
            }
        }
    }
    

提交回复
热议问题