1

I am working on a login app using FirebaseAuth, I am implementing two separate Activities, one to work online and other for offline. There is third starter activity that tries to automatically login a user if his data is present on the SharedPreferences. When finish() is called on an activity signInWithEmailAndPassword won't stop. when internet is not working and "Work Offline" button is clicked , it will lead the user to ActivityOffline. But when internet is restored while the user is on ActivityOffline , out of nowhere ActivityOnline will pop up because of signInWithEmailAndPassword on the starter activity which is already finished.

How can i stop signInWithEmailAndPassword when my Work Offline button is clicked?

public class Starter extends AppCompatActivity {
    Button useOffline;
    String email;
    String password;
    FirebaseAuth mAuth = FirebaseAuth.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starter);
        Prefs prefs = new Prefs(this);
        email = prefs.getStringEntry("email");
        password = prefs.getStringEntry("password");

        useOffline = findViewById(R.id.use_offline);

        useOffline.setOnClickListener(v -> {
            Intent intent = new Intent(Starter.this, OfflineActivity.class);
            startActivity(intent);
            finish();
        });

        final Handler handler = new Handler(Looper.getMainLooper());
        handler.postDelayed(() -> useOffline.setVisibility(View.VISIBLE), 5000);


        if (email.equals("") || password.equals("")) {
            Intent intent = new Intent(Starter.this, LoginActivity.class);
            startActivity(intent);
            finish();
        } else {
            mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(task -> {
                if (task.isSuccessful()) {
                    prefs.setStringEntry("UID", Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid());
                    Intent intent = new Intent(Starter.this, OnlineActivity.class);
                    startActivity(intent);
                } else {
                    prefs.removeEntry("email");
                    prefs.removeEntry("password");
                    Intent intent = new Intent(Starter.this, LoginActivity.class);
                    startActivity(intent);
                }
                finish();
            });

        }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    Firebase already persists the sign-in state of the user, and restores is when the app is restarted. To make sure you detect this automatic re-sign in use an auth state listener as I've shown here: https://stackoverflow.com/collectives/google-cloud/articles/68104924/listen-for-authentication-state-in-android – Frank van Puffelen May 28 '22 at 23:20
  • [`Task`](https://developers.google.com/android/reference/com/google/android/gms/tasks/Task) can only be canceled by the user, while using two `Activity` is commonly not required. – Martin Zeitler May 29 '22 at 00:48
  • I think that this [answer](https://stackoverflow.com/questions/50885891/one-time-login-in-app-firebaseauth) will also help. – Alex Mamo May 29 '22 at 09:22

1 Answers1

0

You can set a boolean flag to true when offline button press and check this flag before start online activity.

Hamid-Ghasemi
  • 275
  • 2
  • 6