0

Currently my app starts with Splash Activity and checks if user is on Firebase Authentication. And if the user's on the authentication i take him to MainActivity.class. If the user's not, i take him to Login Activity.class then after the signup, it takes me to MainActivity.class.

When I first start the app, of course the app will take me to Login Activity. And i authenticate myself and go to MainActivity screen. The problem is when i press back button, it takes me to Login Activity.class screen. I want to just exit the app. How can I fix the problem? This is my code for SplashActivity.



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;

public class SplashActivity extends Activity {
    private FirebaseAuth mAuth = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        try {
            Thread.sleep(2000); 
        } catch (InterruptedException e) {
            e.printStackTrace();
        }



        mAuth = FirebaseAuth.getInstance();
        FirebaseUser user = mAuth.getCurrentUser();
        if (user!= null){
            startActivity(new Intent(SplashActivity.this, MainActivity.class));
        }
        else{
            startActivity(new Intent(SplashActivity.this, LoginActivity.class));
        }

        finish();
    }
}
Jinhyung
  • 43
  • 6
  • did you call finish inside LoginActivity when user login success? – Công Hải May 16 '20 at 08:44
  • Call finish() after calling `startActivity()`. You can also use `moveTaskToBack(true) Process.killProcess(myPid()) exitProcess(1)` in the `onBackPressed()`. This will quit your app. – Lalit Fauzdar May 16 '20 at 08:50
  • Does this answer your question? [How to finish current activity in Android](https://stackoverflow.com/questions/5000787/how-to-finish-current-activity-in-android) – Lalit Fauzdar May 16 '20 at 08:54

2 Answers2

0

Add finish(); after start activity

tohid noori
  • 221
  • 1
  • 11
  • Thank you for the comment. I found another solution for it. Is there difference between stating noHistory for LoginActivity in the Manifest? – Jinhyung May 16 '20 at 08:50
0

You should call finish(); in LoginActivity.

Because when you starts the app without authentication you go on LoginActivity and after login on MainActiviy, so when you press back button you go back to LoginActivity not to SplashScreen.

  • Thank you for the comment. I found another solution for it. Is there difference between stating noHistory for LoginActivity in the Manifest? Again Thanks – Jinhyung May 16 '20 at 08:51