How to change start Activity dynamically?

强颜欢笑 提交于 2019-12-09 08:50:41

问题


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.


回答1:


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();



回答2:


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.




回答3:


It is not necessary that an Activity must have a layout file. You can have a condition check in your launcher activity and redirect to other activity based on the condition. (The transition from launcher activity to condition activity will not be visible though).

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent;
if (condition) {
   intent = new Intent(this, FirstClass.class);
} else {
   intent = new Intent(this, SecondClass.class);
}
startActivity(intent);
finish();
// note we never called setContentView()
}

Other Activity (FirstClass / SecondClass ):

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}



回答4:


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




回答5:


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);



回答6:


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.




回答7:


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");
    }


}


来源:https://stackoverflow.com/questions/15697735/how-to-change-start-activity-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!