问题
I have Profile class where the user enters his name and email. but whenever I open the app it asks the user to enter name. how do I skip this and make it show it to the user only once. I can use the if username != null method but every time user opens app it fetches data from database which makes the app slower. so how do I make the app show the profile activity only the first time and from the 2nd time he should be taken directly to the home screen.
Please help... Thanks in advance
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();
}
});
回答1:
You can use Shared Preferences.
Upon input of the username, when the user click button to next, call this:
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();
And on your 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();
}
回答2:
You can use shared preference for store the boolean value. Once user signed in change the boolean state, Next time check that boolean value before executing your code
来源:https://stackoverflow.com/questions/48457952/how-to-skip-the-signup-activity-and-proceed-to-the-homescreen-in-android