我是Android新手。我必须设计一个应用程序,用户可以在应用程序中输入他/她自己的个人资料。我想要发生的是,在用户安装应用程序后,用户将被重定向到 ProfileActivityUI 以输入他/她的个人资料。之后,他/她将被重定向到应用程序的 MainMenuActivityUI 。
我的问题是,用户在应用程序上输入他/她的个人资料信息后,当他/她关闭应用程序并再次启动应用程序时,他/她将再次被重定向到 ProfileActivityUI (他/她不应该因为他/她已经输入了他/她的个人资料而导致他/她不应该这样做。应该将用户重定向到 MainMenuActivityUI 。
ProfileActivityUI 应该只出现一次。那是在安装之后。但是当用户再次打开应用程序时。它不应再出现了。
我需要你的帮助。因为我不知道该怎么做。示例代码表示赞赏。谢谢。
答案 0 :(得分:1)
嘿,你需要实现分享偏好: - 1)在ProfileActivityUI上,您需要检查加载时的共享首选项,
SharedPreferences sref;
SharedPreferences.Editor editor;
sref = PreferenceManager.getDefaultSharedPreferences(this);
editor = sref.edit();
String checkstring = sref.getString("RoleID",null);
if(!checkstring==null)
{
Intent i = new intent (this,MainMenuActivityUI.class)
startactivity(i);
}
2)当您成功下载ProfileActivityUI时,请在此代码下方: -
SharedPreferences sref;
SharedPreferences.Editor editor;
sref = PreferenceManager.getDefaultSharedPreferences(this);
editor = sref.edit();
editor.putString("RoleID", RoleID);
editor.commit();
Intent i = new Intent(this,MainMenuActivityUI.class);
startActivity(i);
finish();
然后你得到你想要的100%
答案 1 :(得分:0)
你应该尝试使用共享偏好。
public class ProfileActivityUI extends Activity {
SharedPreferences srefforsignout,sref;
Editor edtr;
int signoutcod;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sref=getSharedPreferences("SignOut", MODE_PRIVATE);
signoutcod=sre.getInt("signoutcode", 0);
if(signoutcod==1){
Intent i=new Intent(getApplicationContext(), MainMenuActivityUI.class);
startActivity(i);
finish();
}
srefforsignout=getSharedPreferences("SignOut", MODE_PRIVATE);
edtr=srefforsignout.edit();
buttonsignin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(ProfileActivityUI.this,MainMenuActivityUI.class);
edtr.putInt("signoutcode", 1);
edtr.commit();
startActivity(i);
});
}
}
答案 2 :(得分:0)
我认为SharedPreferences
会解决您的问题,尝试这样做。
首先制作一个启动屏幕(启动画面),这样您就可以检查用户注册的背景。
public class SplashScreen extends Activity {
Thread td;
public static final String MyPREFERENCES = "MyPrefs";
String name, pass;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
td = new Thread(new Runnable() {
@SuppressWarnings("static-access")
@Override
public void run() {
// TODO Auto-generated method stub
try {
td.sleep(3000);
SharedPreferences sharepPreferences = getSharedPreferences(
MyPREFERENCES, Context.MODE_PRIVATE);
//Make sure You have entered same key in my case "email" or "password"..
name = sharepPreferences.getString("email", null);
pass = sharepPreferences.getString("password", null);
if (name == null) {
if (pass == null) {
Intent i = new Intent(SplashScreen.this,
ProfileActivityUI.class);
startActivity(i);
}
} else {
Intent i1 = new Intent(SplashScreen.this,
MainMenuActivityUI.class);
startActivity(i1);
}
finish();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
});
td.start();
}
现在,每当用户在安装应用程序后启动应用程序时,共享首选项中将没有数据,因此它将被重定向到 ProfileActivityUI 。
现在,在 ProfileActivityUI 中,您必须保存用户在SharedPreferences
中输入的数据。
在 ProfileActivityUI
中执行此操作 SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs";
//确保两项活动都相同。
sharedPreferences = getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", et1.getText().toString());
editor.putString("password", et4.getText().toString());
editor.commit();
这将永久存储您的数据。 现在,如果您再次打开应用程序,它会在启动画面后直接将您重定向到 ProfileActivityUI 。
答案 3 :(得分:-1)
我要做的是从MainMenuActivityUI开始,如果未保存配置文件,则自动将他重定向到ProfileActivityUI。 如果UI不是很复杂,你总是可以为一个或另一个视图创建一个setContentView(但它有点混乱......)