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.
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();
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);
}
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.
Use preferences to store the values(Conditions ) which you want to have . then according to that change the startActivity.
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);
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.
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");
}
}
© 2022 - 2024 — McMap. All rights reserved.