简体   繁体   中英

I want to destroy activities on starting a new intent

I have two activities MainActivity and signUp. MainActivity is created when application starts and it gets firebase authentication instance and if no user is found goes to signUp.

public class MainActivity extends AppCompatActivity {

Intent intent;

FirebaseAuth.AuthStateListener authStateListener;
FirebaseAuth firebaseAuth;

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

    Log.d("MainActivity","Created..");

    new Handler().postDelayed(() -> {

        firebaseAuth = FirebaseAuth.getInstance();

        authStateListener = firebaseAuth -> {
            FirebaseUser user = firebaseAuth.getCurrentUser();
            if(user!=null) {
                intent = new Intent(MainActivity.this, HomeScreen.class);
                Log.d("Authentication", "User already signed in..");
                intent.putExtra("state", "alreadySignIned");
                startActivity(intent);
                MainActivity.this.finish();
            }
        };

        intent = new Intent(this,signUp.class);
        startActivity(intent);
        MainActivity.this.finish();

        firebaseAuth.addAuthStateListener(authStateListener);
    },5000);
}

}

Now when new user is registered in signUp it goes to Homescreen activity but the problem I am facing is that authStateListener is getting the user when new user is signed up and starting intent from MainActivity.

public class signUp extends AppCompatActivity {

Intent intent;

FirebaseAuth firebaseAuth;
boolean buttonPress = false;

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

    EditText emailUp,passUp,userName;
    Button signUp;
    TextView toSignIn;

    emailUp = findViewById(R.id.signUpEmail);
    passUp = findViewById(R.id.signUpPass);
    signUp = findViewById(R.id.signUpButton);
    userName = findViewById(R.id.signUpUserName);
    toSignIn = findViewById(R.id.toSignIn);

    firebaseAuth = FirebaseAuth.getInstance();

    signUp.setOnClickListener(v -> {
        buttonPress = true;
        String email = emailUp.getText().toString();
        String pass = passUp.getText().toString();
        String name = userName.getText().toString();

        if(email.isEmpty()){
            emailUp.setError("Please enter email id..");
            emailUp.requestFocus();
        } else if(pass.isEmpty()){
            passUp.setError("Please enter password..");
            passUp.requestFocus();
        } else {
            firebaseAuth.createUserWithEmailAndPassword(email,pass).addOnCompleteListener(task -> {
                if(!task.isSuccessful()){
                    Toast.makeText(signUp.this,"Sign up Unsuccessful, please try again.",Toast.LENGTH_SHORT).show();
                } else {
                    Log.d("Authentication","User signing up..");
                    intent = new Intent(this,HomeScreen.class);
                    intent.putExtra("state","signedUp");
                    intent.putExtra("name",name);
                    startActivity(intent);
                    signUp.this.finish();
                }
            });
        }
    });

}

}

Here is the log from Homescreen activity

2020-05-18 14:04:33.355 29696-29696/com.example.movietracker I/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@8f143e5
2020-05-18 14:05:04.668 29696-29711/com.example.movietracker D/FirebaseAuth: Notifying id token listeners about user ( 82Q12yEqqjUP0AWGuGoxok4pvae2 ).
2020-05-18 14:05:04.669 29696-29711/com.example.movietracker D/FirebaseAuth: Notifying auth state listeners about user ( 82Q12yEqqjUP0AWGuGoxok4pvae2 ).
2020-05-18 14:05:04.669 29696-29696/com.example.movietracker D/Authentication: User already signed in..
2020-05-18 14:05:04.734 29696-29696/com.example.movietracker D/Homescreen Activity: Created..
2020-05-18 14:05:04.734 29696-29696/com.example.movietracker D/Intent: State alreadySignIned
2020-05-18 14:05:04.771 29696-29696/com.example.movietracker D/Cursor: Count on create 0
2020-05-18 14:05:04.773 29696-29696/com.example.movietracker D/Homescreen Activity: Started..
2020-05-18 14:05:04.773 29696-29696/com.example.movietracker D/Username: Username null
2020-05-18 14:05:04.819 29696-29696/com.example.movietracker D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-05-18 14:05:04.826 29696-29696/com.example.movietracker D/WatchList DB: Size 0
2020-05-18 14:05:04.826 29696-29696/com.example.movietracker D/WatchListID: Size 0
2020-05-18 14:05:04.838 29696-29696/com.example.movietracker D/Authentication: User signing up..

Here it is notifying authStateListener on sign up and after some code it is taking intent from signUp class. I want it to destroy MainActivity and directly take intent from signUp without notifying authStateListener.

Please help.

You can destroy an activity like this:

Step 1: Declare a Activity variable in MainActivity

public static Activity MainActivityContext;

Step 2: Initialize MainActivityContext variable inside OnCreate Method of MainActivity

MaMainActivityContext = this;

Step 3: Now go to the java statement where you want to destroy MainActivity and write

MainActivity.MainActivityContext.finish();

MainActivity starts signup Activity always. In the case where the user already exists, HomeScreen Activity is also launched. This is probably not what you want.

Also, the authListener callback calls MainActivity.this.finish() but by this time, MainActivity has already been finished so this is also wrong.

If you don't want the authListener to trigger when the user signs up, you should remove the listener before creating the user.

Generally speaking your code could be improved. You should look at some more tutorials for a better idea of how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM