How to change start Activity dynamically?

2019-03-24 16:35发布

I am working on an android app. I want to change Start activity dynamically. i mean when user start app first time then start activity will different and when start second time start activity change.This will skip first two activity and move to third activity .how can i achieve this.

7条回答
霸刀☆藐视天下
2楼-- · 2019-03-24 16:56

No matter what first open your app in a Main Activity. Meanwhile use SharedPreference to save the data on how many times you have loaded the app.

You gonna have to do something as below in your

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    String dataAvailable;
    SharedPreferences prefs = getSharedPreferences("countPref", Context.MODE_PRIVATE);
    dataAvailable = prefs.getString("dataAvailable", null);

    //checking whether launching for the first time.
    if(dataAvailable!=null){
         int appLoadedCount = prefs.getInt("appLoadedCount", -1);
         appLoadedCount++;
         prefs.edit().putInt("appLoadedCount", appLoadedCount).commit();

         // Check how many times loaded
         if(appLoadedCount==0){
             Intent firstAct = new Intent(MainActivity.this, FirstActivity.class);
             startActivity(firstAct);
         }
         else if(appLoadedCount==1){
             Intent scndAct = new Intent(MainActivity.this, ScndActivity.class);
             startActivity(scndAct);
         }
         else if(appLoadedCount==2){
             Intent thirAct = new Intent(MainActivity.this, ThirdActivity.class);
             startActivity(thirAct);
         }
         else{
             Intent thirAct = new Intent(MainActivity.this, ThirdActivity.class);
             startActivity(thirAct);
         }

         Log.v("avilable", dataAvailable);
         Log.v("avilable", String.valueOf(appLoadedCount));
    }
    else{
         //loading first time
         prefs.edit().putString("dataAvailable", "yeap").commit();
         //setting the count to 1 as loaded for the firs time
         prefs.edit().putInt("appLoadedCount", 0).commit();
         Log.v("Not avilable", "Loaded first time");
    }


}
查看更多
ゆ 、 Hurt°
3楼-- · 2019-03-24 16:58

you Can Use SharedPreference according to your Requirement.

you can Store and Retrieve Values from this Link

Inside each Oncreate() method of your Activity you can Check for the SharedPreference Value and start your Activity there.

Hope it will Help you.

查看更多
小情绪 Triste *
4楼-- · 2019-03-24 17:00

You cannot change the first activity dynamically, but you can create a transparent activity like this:

<activity
    android:name=".ActivityLauncher"
    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

and select next activity in the onCreate method:

if ( logged() ) {
    intent = new Intent(this,MainActivity.class);
} else {
    intent = new Intent(this,SignInActivity.class);
}
startActivity(intent);
finish();
查看更多
等我变得足够好
5楼-- · 2019-03-24 17:04

This is what I do when a user has selected a Remember Me check box on the Login Screen.

To save value to the SharedPreference file:

You will need to do something like this once in the first Activity and once in the second Activity

sharedPrefs = getApplicationContext().getSharedPreferences(PRIVATE_PREF, Context.MODE_PRIVATE);

// EDITOR INSTANCE TO SAVE THE NAG SETTING
editor = sharedPrefs.edit();

// GET THE NAG SETTING CHECKBOX
if (chkbxNagSetting.isChecked())    {

    editor.putBoolean(NAG_SETTING, true);
} else {
    editor.putBoolean(NAG_SETTING, false);
}

editor.commit();

To retrieve the value from the SharedPreference file:

boolean blNagSetting = sharedPrefs.getBoolean(NAG_SETTING, false);

if (blNagSetting == true)   {
    Intent startMainPage = new Intent(SignIn.this, SplashScreen.class);
    startMainPage.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(startMainPage);
    finish();
}

And these are the necessary Global variables / instances used in the Activity:

SharedPreferences sharedPrefs;
Editor editor;
private static final String PRIVATE_PREF = "CHANGE_TO_SOME_FILE_NAME";
private static final String NAG_SETTING = "nag_setting";

You will have to modify the code slightly to account for skipping 2 Activities.

Let me know if you need any help.

查看更多
家丑人穷心不美
6楼-- · 2019-03-24 17:07

Use preferences to store the values(Conditions ) which you want to have . then according to that change the startActivity.

查看更多
Fickle 薄情
7楼-- · 2019-03-24 17:09

Use sharedpreference to first time they logged or not

  if (!checkNameInfo()) {
//first time
                    FirstActivity();
                } else {
//second time
                    Intent i = new Intent(first.this, second.class);
                    startActivity(i);
                    finish();
                }

Check the value

private final boolean checkNameInfo() {
        boolean role = mPreferences.contains("Name");
        if (role) {
            return true;
        }
        return false;
    }

IN firstActivity

SharedPreferences.Editor editor = mPreferences.edit();
                editor.putString("Name", edt.getText().toString()); 
editor.commit();
Intent i = new Intent(first.this, second.class);
                        startActivity(i);
查看更多
登录 后发表回答