[英]How to start an android app from two activity
我正在开发一个 android 应用程序,我在启动屏幕上用我的手机号码注册。 我想要的是,在安装应用程序时只需要一次和第一次注册。 安装应用程序后,应用程序应该从另一个活动打开,而不是从初始屏幕。 怎么可能。
成功注册登录后,您必须将数据存储在SharedPreferences
。
AppTypeDetails 是 SharedPreferences 的类。
AppTypeDetails.getInstance(SignUpActivity.this).setEmail(<Your Email ID>);
AppTypeDetails.getInstance(SignUpActivity.this).setPassword(<Your Password>);
AppTypeDetails.java
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
public class AppTypeDetails {
private SharedPreferences sh;
private AppTypeDetails() {
}
private AppTypeDetails(Context mContext) {
sh = PreferenceManager.getDefaultSharedPreferences(mContext);
}
private static AppTypeDetails instance = null;
/**
*
* @param mContext
* @return {@link AppTypeDetails}
*/
public synchronized static AppTypeDetails getInstance(Context mContext) {
if (instance == null) {
instance = new AppTypeDetails(mContext);
}
return instance;
}
// get username
public String getEmail() {
return sh.getString("email", "");
}
public void setEmail(String email) {
sh.edit().putString("email", email).commit();
}
// get password
public String getPassword() {
return sh.getString("password", "");
}
public void setPassword(String password) {
sh.edit().putString("password", password).commit();
}
public void clear() {
sh.edit().clear().commit();
}
}
现在在启动画面中检查以下代码。
String email = AppTypeDetails.getInstance(SplashScreen.this).getEmail();
String pass = AppTypeDetails.getInstance(SplashScreen.this).getPassword();
if (email.trim().isEmpty() && pass.trim().isEmpty()) {
Intent intent = new Intent(SplashScreen.this, Login.class);
startActivity(intent);
} else {
Intent intent = new Intent(SplashScreen.this, MainScreenTabHost.class);
startActivity(intent);
}
对于明确的SharedPreferences
:
注销时调用 clear() 方法。
在启动类中,您应该检查应用程序是否第一次运行。 如果是,则继续,如果不是,则开始第二个活动。 可以通过检查布尔值,将其存储在共享首选项中并每次在启动时检查其值。
尝试使用 SharedPrefence。但这不是完整的答案。显示您的 SplashScreen 检查您的用户是否注册。
Boolean REG_RESPONCE = new Session_manag(getActivity()).IsSessionCheckOrCreated();
if (REG_RESPONCE.equals(true)) {
Intent toHomeactivity = new Intent(Splash.this, MainMenu.class);
finish();
startActivity(toHomeactivity);
} else {
Intent i = new Intent(Splash.this, SignUp.class);
finish();
startActivity(i);
}
您可以在初始屏幕上检查用户是否已使用号码注册的条件。为此,您必须将该号码保存在 SharedPreferences 中。 请按照以下步骤操作,
第 1 步:当用户第一次打开应用时会出现闪屏。在这里你可以检查数字的条件。第一次,当用户打开应用时,SharedPreferences.so 中不存在应用值(数字)。所以应用会要求输入数字。 当用户输入数字并提交时,将其存储在 SharedPreferences 中。
第 2 步:现在,当用户第二次出现在启动画面时,该条件变为真,因为 SharedPreferences 具有 Value(number)。 这样您就可以在第二个活动中重定向应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.