我正在开发一个android应用程序。我想动态更改开始活动。我的意思是,当用户第一次启动应用程序,然后启动活动将不同,当第二次启动活动change.This将跳过前两个活动,并转移到第三个活动。我可以实现这一点。如何动态更改开始活动?
13
A
回答
0
使用首选项来存储您想要的值(条件)。然后根据那个改变startActivity。
1
您可以根据您的要求使用SharedPreference。
可以存储和检索该Link
值内,您的每个活动Oncreate()
方法,你可以检查SharedPreference值并且启动活动。
希望它能帮助你。
0
使用sharedpreference到他们第一次登录或没有
if (!checkNameInfo()) {
//first time
FirstActivity();
} else {
//second time
Intent i = new Intent(first.this, second.class);
startActivity(i);
finish();
}
检查值
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);
0
这是当用户选择了我做什么在登录屏幕上记住我复选框。
要保存价值的SharedPreference
文件:
您需要在第二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();
做这样的事情曾经在第一Activity
和一次是从SharedPreference
文件检索值:
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();
}
而这些是在中使用的必要的全局变量/实例:
SharedPreferences sharedPrefs;
Editor editor;
private static final String PRIVATE_PREF = "CHANGE_TO_SOME_FILE_NAME";
private static final String NAG_SETTING = "nag_setting";
你需要稍微修改代码以占跳过2 Activities
。
让我知道你是否需要任何帮助。
0
无论首先在主要活动中打开您的应用程序。同时使用SharedPreference来保存您已加载应用程序的次数。
你要去必须在下面做一些事情你
@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");
}
}
28
不能动态改变的第一个活动,但您可以创建这样一个透明的活动:
<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>
,并选择下一个活动在onCreate
方法中:
if (logged()) {
intent = new Intent(this,MainActivity.class);
} else {
intent = new Intent(this,SignInActivity.class);
}
startActivity(intent);
finish();
0
它不是必需的ary一个Activity必须有一个布局文件。您可以在启动器活动中进行条件检查,并根据条件重定向到其他活动。 (虽然从发射器活动到条件活动的转换将不可见)。
@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()
}
其他活动(的Firstclass /二等):
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
相关问题
- 1. android如何更改开始活动
- 2. android更改开始活动
- 3. 动态活动开始时的布局更改
- 4. 如何动态更改活动类
- 5. 更改Android应用开始活动
- 6. 如何从singleInstance活动开始活动?
- 7. 从标题名称更改的主要活动开始活动
- 8. 如何开始活动
- 9. 如何开始活动?
- 10. 开始活动
- 11. 活动A开始活动B,如何关闭活动A
- 12. 如何开始动画结束活动
- 13. 从开始开始活动
- 14. 更改Android开始活动后,应用程序不会启动
- 15. 动态从可扩展列表动态地开始活动
- 16. 动态更改活动的布局
- 17. 动态更改默认活动
- 18. 动态更改活动主题
- 19. Android开始活动
- 20. 活动未开始
- 21. 重新开始活动,例如方向更改
- 22. 查看状态,新的活动开始
- 23. 从静态方法开始活动
- 24. 开始从静态内容的活动
- 25. Android - 在开始新活动时保存活动状态
- 26. 改善活动开始时间(AsyncTask?)
- 27. 开始活动,不完成活动
- 28. 活动已移至开始活动
- 29. 从非活动开始活动
- 30. 主要活动layoutInflater,活动不开始
它运作良好。 –
它需要布局吗? –