我有Profile类,用户输入他的姓名和电子邮件。但每当我打开应用程序时,它会要求用户输入名称。如何跳过这个并让它只向用户显示一次。我可以使用if username != null
方法,但每次用户打开应用程序时,它都会从数据库中获取数据,这会使应用程序变慢。那么如何让应用程序仅在第一次显示配置文件活动时,从第二次开始直接进入主屏幕。
请帮助......提前致谢
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setup_profile);
storage = FirebaseStorage.getInstance().getReference();
progress = new ProgressDialog(this);
input_name = (EditText) findViewById(R.id.name);
input_email = (EditText) findViewById(R.id.email);
input_status = (EditText) findViewById(R.id.status);
input_quote = (EditText) findViewById(R.id.quote);
input_ph = (EditText) findViewById(R.id.field_phone_number);
imageView = (ImageView) findViewById(R.id.imageView);
addData = (Button) findViewById(R.id.save);
SharedPreferences sp = getSharedPreferences("com.your.package", Context.MODE_PRIVATE);
boolean hasUsername = sp.getBoolean("has_username", false);
if (hasUsername) { //checks if the user already input username to skip this activity
Intent intent = new Intent(SetUpProfile.this, HomeScreen.class);
startActivity(intent);
finish();
}
FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();
final DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getPhoneNumber());
addData.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
SetUpProfileHelper user;
user = new SetUpProfileHelper(input_name.getText().toString(), input_email.getText().toString(), input_status.getText().toString(), input_quote.getText().toString());
ref.child("Name").setValue(input_name.getText().toString());
ref.child("Email").setValue(input_email.getText().toString());
ref.child("Status").setValue(input_status.getText().toString());
ref.child("Quote").setValue(input_quote.getText().toString()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(getApplicationContext(),"Profile Successfully Updated",Toast.LENGTH_SHORT).show();
startActivity(new Intent(SetUpProfile.this, HomeScreen.class));
finish();
}
}
});
SharedPreferences sp = getSharedPreferences("com.appmaster.akash.messageplus", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("has_username", true); //save that the user enters username
editor.apply();
}
});
答案 0 :(得分:0)
您可以使用共享偏好设置。
输入用户名后,当用户点击下一个按钮时,请调用:
SharedPreferences sp = context.getSharedPreferences("com.your.package", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("has_username", true); //save that the user enters username
editor.apply();
在ProfileActivity onCreate()
:
SharedPreferences sp = context.getSharedPreferences("com.your.package", Context.MODE_PRIVATE);
boolean hasUsername = sp.getBoolean("has_username", false);
if (hasUsername) { //checks if the user already input username to skip this activity
Intent intent = new Intent(ProfileActivity.this, YourNextActivity.class);
startActivity(intent);
finish();
}
答案 1 :(得分:0)
您可以使用共享首选项来存储布尔值。用户登录后更改布尔状态,下次在执行代码之前检查该布尔值