2

The situation is the following:

The activity started on app's launch (let's call it splash activity, because used for some checkings and for read/write some settings) is made by frameworks and the last one of them has firebase signInWithEmailAndPassword whose addOnCompleteListener contains the code for starting a new activity (let's say the main activity).

one of the options of the (so called) main activity's menu is firebase signOut, when i tap it the current user signs out but the signInWithEmailAndPassword (coded into the last fragment of previous activity) is called again!

is it because i splitted sign-in and sign-out in two different activities?

can anybody kindly help me with that, thanks in advance.

here is code schema:

FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                if (task.isSuccessful()) {
                    // read some data from realtime database
                    database
                        .getReference(path)
                        .addValueEventListener(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                get snapshot data ...

                                // Store them into shared preferences
                                mSettings
                                    .edit()
                                    .putStringSet("STRING_SET_KEY", dataSet)
                                    .apply();

                                // Update some Realtime database data
                                database
                                    .getReference(path)
                                    .updateChildren(newData)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                            public void onComplete(@NonNull Task<Void> task) {
                                                if (task.isSuccessful()) {
                                                    // Start new activity 
                                                    Intent intent = new Intent(getActivity(), MainActivity.class);
                                                    startActivity(intent);
                                                }
                                            });
                                    });
                                });
                        });
                    }
                });
        });

p.s. - i noticed because the signOut() kicks out the firebase user and when the signInWithEmailAndPassword is called again i got an error because it's not possible to read realtime database

Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46
r08y
  • 125
  • 1
  • 13
  • the code shema is at the button or at the onCreate ? – Ticherhaz FreePalestine Jan 12 '19 at 17:22
  • that code is in the last fragment of the first activity, the `signInWithEmailAndPassword` is called from a fragment – r08y Jan 12 '19 at 17:55
  • Can you show us the code where you put it? – Ticherhaz FreePalestine Jan 12 '19 at 17:57
  • there's not really much else to show, inside a `public class FragmentN extends Fragment` there are `onCreateView` and `onActivityCreated`, that piece of code is inside `onActivityCreated`; nothing else. – r08y Jan 12 '19 at 18:03
  • I see what is your problem there, the code segment is suppose to be at the button Sign In listener only. If you want to check the user is already signed in or not, you need to use the code at answer below. I updated the asnwer – Ticherhaz FreePalestine Jan 12 '19 at 18:06
  • 1
    You can also check [this](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth). – Alex Mamo Jan 13 '19 at 09:31
  • thanks @Alex Mamo, finishing the activity did the job. Only one more question (if you can reply) it this normal way or it's a workaround? – r08y Jan 14 '19 at 06:55

1 Answers1

0

You may check whether the user is still sign in or not.

According to this link: https://firebase.google.com/docs/auth/android/manage-users

This code put at the onCreateView...

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        // User is signed in
          // Start new activity 
         Intent intent = new Intent(getActivity(), MainActivity.class);
         startActivity(intent);
    } else {
        // No user is signed in
    }
Ticherhaz FreePalestine
  • 2,738
  • 4
  • 20
  • 46