0

After I sign in with PhoneNumber Auth it's will take me into Profile activity, I want that stay log in into the application if I go out and close it from used apps and come back again to the application that I won't be still log in.

the following the Java Code of PhoneNumber Auth and the Profile

VerifyPhoneActivity.Java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_verify_phone);

    mAuth = FirebaseAuth.getInstance();

    editText = findViewById(R.id.editTextCode);

    String phonenumber = getIntent().getStringExtra("phoneNumber");
    sendVerificationCode(phonenumber);

    findViewById(R.id.buttonSignIn).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String code = editText.getText().toString().trim();

            if (code.isEmpty() || code.length()<6){

                editText.setError("enter Code...");
                editText.requestFocus();
                return;

            }

            verifyCode(code);

        }
    });


}

private void verifyCode(String code){

    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(verificationId, code);
    signInWithCredential(credential);
}

private void signInWithCredential(PhoneAuthCredential credential) {
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()){

                        Intent intent = new Intent(VerifyPhoneActivity.this, List.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

                        startActivity(intent);

                    }else{
                        Toast.makeText(VerifyPhoneActivity.this, task.getException().getMessage(),Toast.LENGTH_LONG).show();
                    }
                }
            });

}

private void sendVerificationCode(String phonenumber){
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phonenumber,
            10,
            TimeUnit.SECONDS,
            this,
            mCallbacks

    );
}

private PhoneAuthProvider.OnVerificationStateChangedCallbacks
        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {

    @Override
    public void onCodeSent(String s, PhoneAuthProvider.ForceResendingToken forceResendingToken) {
        super.onCodeSent(s, forceResendingToken);
        verificationId = s;
    }

    @Override
    public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
        String code = phoneAuthCredential.getSmsCode();
        if (code != null){
            editText.setText(code);
            verifyCode(code);
        }

    }

    @Override
    public void onVerificationFailed(FirebaseException e) {

        Toast.makeText(VerifyPhoneActivity.this,e.getMessage(), Toast.LENGTH_LONG).show();

    }
};

Child_Profile.Java

public class Child_Profile extends AppCompatActivity {
private TextView name;
private TextView gender;
private TextView birth;
private TextView bload;
private TextView Text_Dates;
private TextView Text_Time;
private Button Button_record;
private TextView Text_record;
private TextView Text_hospial;
private TextView Text_Plan;
private TextView Text_Satus;
private TextView Text_Price;
private ImageView ImageView_choose_image;
private Button button_choose_image;
private Uri mUri;
private static final int GALLERY_INTENT = 1;


private StorageReference mStorage;
private DatabaseReference mData;
private FirebaseFirestore db = FirebaseFirestore.getInstance();

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_sign_out)
        signOut();
    return true;
}

private void signOut() {
    AuthUI.getInstance().signOut(this)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    startActivity(new Intent(Child_Profile.this, User.class));
                    finish();
                }
            });
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_child__profile);

    name = findViewById(R.id.text_view_Namee);
    gender = findViewById(R.id.text_view_Gender);
    birth = findViewById(R.id.text_view_Birth);
    bload = findViewById(R.id.text_view_Blood);
    Text_Dates = findViewById(R.id.Text_Dates);
    Text_Time = findViewById(R.id.Text_Time);
    Text_record = findViewById(R.id.Text_record);
    Text_hospial = findViewById(R.id.Text_hospial);
    Text_Plan = findViewById(R.id.Text_Plan);
    Text_Satus = findViewById(R.id.Text_Satus);
    Button_record = findViewById(R.id.Button_record);
    Text_Price = findViewById(R.id.Text_Price);
    button_choose_image = findViewById(R.id.button_choose_image);
    ImageView_choose_image = findViewById(R.id.ImageView_choose_image);
    mStorage = FirebaseStorage.getInstance().getReference("Photo");
    mData = FirebaseDatabase.getInstance().getReference("Photo");


    button_choose_image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            openFileChooser();

        }
    });

    Intent in = getIntent();
    final Bundle b = in.getExtras();
    if (b != null) {

        String n = (String) b.get("id");

        db.collection("Child Profile").document(n).get()
                .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                    @Override
                    public void onSuccess(DocumentSnapshot documentSnapshot) {
                        String pName = documentSnapshot.getString("name");
                        String pgender = documentSnapshot.getString("gender");
                        String pbirth = documentSnapshot.getString("birth");
                        String pbload = documentSnapshot.getString("bload");
                        String pDates = documentSnapshot.getString("dates");
                        String pTime = documentSnapshot.getString("time");
                        String pHospital = documentSnapshot.getString("hospital");
                        String pPlan = documentSnapshot.getString("typeOfPlan");
                        String pSatus = documentSnapshot.getString("satus");
                        String pPrice = documentSnapshot.getString("price");


                        name.setText(pName);
                        gender.setText(pgender);
                        birth.setText(pbirth);
                        bload.setText(pbload);
                        Text_Dates.setText(pDates);
                        Text_Time.setText(pTime);
                        Text_hospial.setText(pHospital);
                        Text_Plan.setText(pPlan);
                        Text_Satus.setText(pSatus);
                        Text_Price.setText(pPrice);


                    }
                });

    }


}

List.Java

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_List);

    mAuth = FirebaseAuth.getInstance();
    setUpRecyclerView();

    FloatingActionButton aaa = findViewById(R.id.button_add_profile);
    aaa.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(), NewChild.class);
            startActivity(i);
        }
    });

}


private void setUpRecyclerView() {
    Query query = profileRef.whereEqualTo("user_id", FirebaseAuth.getInstance().getCurrentUser().getUid());


    FirestoreRecyclerOptions<addp> options = new FirestoreRecyclerOptions.Builder<addp>()
            .setQuery(query, addp.class)
            .build();

    adapter = new padapter(options);

    RecyclerView recyclerView = findViewById(R.id.recycler_View);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

 adapter.setOnItemClickListener(new padapter.OnItemClickListener()

{
    @Override
    public void onItemClick (DocumentSnapshot documentSnapshot,int position){
    String id = documentSnapshot.getId();
    Toast.makeText(Profile.this, "Position: " + position + "ID" + id, Toast.LENGTH_SHORT).show();

    Intent i = new Intent(getApplicationContext(), Child_Profile.class);
        i.putExtra("id", id);



        startActivity(i);

}
});
Rarblack
  • 4,559
  • 4
  • 22
  • 33

1 Answers1

0

ProfileActivity set as launcher activity and add line of code at onCreate of ProfileAtivity

 FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
 if(user==null){
 Intent intent=new Intent(ProfileActivity,this,LoginActivity.class);
 startActivity(intent);
 finish()
 }
  else{
     //stay here
      }`
Tarun Pal
  • 24
  • 1
  • 4
  • can you please explain more i don't understand – Abdullah AMB Oct 28 '18 at 11:49
  • you want to stay at profile activity, set your profile activity as launcher activity and put this code on profile activity. when user come on your launcher activity(profile activity) its check for user is logged in or out ,if login then stay at profile activity,if logout back to login activity. – Tarun Pal Oct 28 '18 at 12:19