I am building an Android app. I worked on sign-in and signup activities. I have different activities for sign-in and signup and have one main activity. Now, when initially I connected the Firebase database with the project and implemented the signup things it worked fine (just showed an dialogue and toast as I wanted). but when I have added sign-in using email/password and with Google also. After that when I looked on signup it started to behave strangely. First, it just shows the Toast and dialogue I had set on the onComplete() of Signup, but now it is also redirecting to the main activity. After I observed that I had put an intent redirecting to the Sign-in activity. Then from sign-in if successful user is redirected to the main activity. But now it’s behavior is not what I wanted on sign up button, it redirects to mainactivity but I want to move to signin activity.
here is the code on sign-up button.
signUpBinding.buttonSignUp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//show progress dialogue
progressDialog.show();
auth.createUserWithEmailAndPassword(
signUpBinding.textEmailsignup.getText().toString(),
signUpBinding.textPasswordsignup.getText().toString())
.addOnCompleteListener(
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
progressDialog.dismiss();
if(task.isSuccessful()){
Intent intent = new Intent(SignUpActivity.this,
SignInActivity.class);
startActivity(intent);
finish();
//using Users constructor for signup
Users users = new Users(
signUpBinding.textUsernameSignUp.getText().toString(),
signUpBinding.textEmailsignup.getText().toString(),
signUpBinding.textPasswordsignup.getText().toString()
);
//to get the UID of user from AuthResult
String id = task.getResult().getUser().getUid();
//setting values in realtime database
firebaseDatabase.getReference().child("Users").
//we can use id and username or anyother string as we
// want in the below child to name the user node.
child(signUpBinding.textUsernameSignUp.getText().toString())
.setValue(users);
Toast.makeText(SignUpActivity.this, "User Created Successfully ",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(SignUpActivity.this, "Error: "+task.getException()
.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
});
}
});
and this is the logcat on click of signup button
{2023-09-04 15:36:43.710 31698-31698 AppTurboService com.sarmadali.chatmingle D getSettingSupport resullt = false mSettingCloudEnable = true
2023-09-04 15:36:43.752 31698-31698 ViewRootIm...g Account] com.sarmadali.chatmingle D hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2023-09-04 15:36:43.724 31698-31698 chatty com.sarmadali.chatmingle I uid=10376(com.sarmadali.chatmingle) identical 2 lines
2023-09-04 15:36:43.724 31698-31698 AppTurboService com.sarmadali.chatmingle D getSettingSupport resullt = false mSettingCloudEnable = true
2023-09-04 15:36:43.757 31698-31698 InputTransport com.sarmadali.chatmingle I Create ARC handle: 0x75177e3ce0
2023-09-04 15:36:43.758 31698-31698 FirebaseAuth com.sarmadali.chatmingle I Creating user with [email protected] with empty reCAPTCHA token
2023-09-04 15:36:43.776 31698-32101 NetworkSecurityConfig com.sarmadali.chatmingle D No Network Security Config specified, using platform default
2023-09-04 15:36:43.781 31698-31734 Surface com.sarmadali.chatmingle D Surface::connect(this=0x7517854000,api=1)
2023-09-04 15:36:43.782 31698-31734 mali_winsys com.sarmadali.chatmingle D EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2023-09-04 15:36:43.782 31698-32101 System com.sarmadali.chatmingle W Ignoring header X-Firebase-Locale because its value was null.
2023-09-04 15:36:43.782 31698-31734 Surface com.sarmadali.chatmingle D Surface::setBufferCount(this=0x7517854000,bufferCount=3)
2023-09-04 15:36:43.783 31698-31734 Surface com.sarmadali.chatmingle D Surface::allocateBuffers(this=0x7517854000)
2023-09-04 15:36:43.795 31698-32101 System.out com.sarmadali.chatmingle I [okhttp]:check permission begin!
2023-09-04 15:36:43.796 31698-32101 System com.sarmadali.chatmingle W ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
2023-09-04 15:36:43.798 31698-32101 System.out com.sarmadali.chatmingle I [okhttp] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils
2023-09-04 15:36:44.460 31698-32101 System.out com.sarmadali.chatmingle I [socket]:check permission begin!
2023-09-04 15:36:44.461 31698-32101 System com.sarmadali.chatmingle W ClassLoader referenced unknown path: system/framework/mediatek-cta.jar
2023-09-04 15:36:44.464 31698-32101 System.out com.sarmadali.chatmingle I [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils
2023-09-04 15:36:46.041 31698-32101 System.out com.sarmadali.chatmingle I [OkHttp] sendRequest<<
2023-09-04 15:36:47.520 31698-32101 System com.sarmadali.chatmingle W Ignoring header X-Firebase-Locale because its value was null.
2023-09-04 15:36:47.538 31698-32101 System.out com.sarmadali.chatmingle I [okhttp]:check permission begin!
2023-09-04 15:36:47.539 31698-32101 System.out com.sarmadali.chatmingle I [okhttp]:not MMS!
2023-09-04 15:36:47.539 31698-32101 System.out com.sarmadali.chatmingle I [okhttp]:not Email!
2023-09-04 15:36:47.544 31698-32101 System.out com.sarmadali.chatmingle I [OkHttp] sendRequest<<
2023-09-04 15:36:48.398 31698-32101 FirebaseAuth com.sarmadali.chatmingle D Notifying id token listeners about user ( ikasJsyb4saAFiS0hYVE1Mcggjh2 ).
2023-09-04 15:36:48.399 31698-32101 FirebaseAuth com.sarmadali.chatmingle D Notifying auth state listeners about user ( ikasJsyb4saAFiS0hYVE1Mcggjh2 ).
2023-09-04 15:36:48.420 31698-31734 Surface com.sarmadali.chatmingle D Surface::disconnect(this=0x7517854000,api=1)
2023-09-04 15:36:48.424 31698-31698 View com.sarmadali.chatmingle D [Warning] assignParent to null: this = DecorView@b4df957[Creating Account]
2023-09-04 15:36:48.425 31698-31698 InputTransport com.sarmadali.chatmingle I Destroy ARC handle: 0x75177e3ce0
2023-09-04 15:36:48.504 31698-31698 ViewRootImpl[Toast] com.sarmadali.chatmingle D hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2023-09-04 15:36:48.509 31698-31698 InputTransport com.sarmadali.chatmingle I Create ARC handle: 0x74b9ec3400
2023-09-04 15:36:48.522 31698-31734 Surface com.sarmadali.chatmingle D Surface::connect(this=0x7517854000,api=1)
2023-09-04 15:36:48.523 31698-31734 mali_winsys com.sarmadali.chatmingle D EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2023-09-04 15:36:48.523 31698-31734 Surface com.sarmadali.chatmingle D Surface::setBufferCount(this=0x7517854000,bufferCount=3)
2023-09-04 15:36:48.523 31698-31734 Surface com.sarmadali.chatmingle D Surface::allocateBuffers(this=0x7517854000)
2023-09-04 15:36:48.531 31698-31698 ActivityThread com.sarmadali.chatmingle W handleWindowVisibility: no activity for token android.os.BinderProxy@c3e4fba
2023-09-04 15:36:48.532 31698-32113 libMEOW com.sarmadali.chatmingle D applied 1 plugins for [com.sarmadali.chatmingle]:
2023-09-04 15:36:48.532 31698-32113 libMEOW com.sarmadali.chatmingle D plugin 1: [libMEOW_gift.so]:
2023-09-04 15:36:48.537 31698-31698 os.typeface com.sarmadali.chatmingle D Already set agent. fontPath:null fontHash:
2023-09-04 15:36:48.644 31698-31698 ActivityThread com.sarmadali.chatmingle W handleWindowVisibility: no activity for token android.os.BinderProxy@1e1f34
2023-09-04 15:36:48.645 31698-32115 libMEOW com.sarmadali.chatmingle D applied 1 plugins for [com.sarmadali.chatmingle]:
2023-09-04 15:36:48.645 31698-32115 libMEOW com.sarmadali.chatmingle D plugin 1: [libMEOW_gift.so]:
2023-09-04 15:36:48.649 31698-31698 os.typeface com.sarmadali.chatmingle D Already set agent. fontPath:null fontHash:
2023-09-04 15:36:48.750 31698-31698 PhoneWindow com.sarmadali.chatmingle V DecorView setVisiblity: visibility = 4, Parent = null, this = DecorView@7397a32[]
2023-09-04 15:36:48.753 31698-31698 ViewRootIm...nActivity] com.sarmadali.chatmingle D hardware acceleration = true , fakeHwAccelerated = false, sRendererDisabled = false, forceHwAccelerated = false, sSystemRendererDisabled = false
2023-09-04 15:36:48.757 31698-31698 InputTransport com.sarmadali.chatmingle I Create ARC handle: 0x74b9ec3c00
2023-09-04 15:36:48.758 31698-31698 PhoneWindow com.sarmadali.chatmingle V DecorView setVisiblity: visibility = 0, Parent = android.view.ViewRootImpl@2ce4539, this = DecorView@7397a32[MainActivity]
2023-09-04 15:36:48.759 28996-18468 MagicButtonManager com.transsion.videocallenhancer E gained class = com.sarmadali.chatmingle.MainActivity
2023-09-04 15:36:48.784 31698-31734 Surface com.sarmadali.chatmingle D Surface::connect(this=0x74ba05f000,api=1)
2023-09-04 15:36:48.786 31698-31734 mali_winsys com.sarmadali.chatmingle D EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, EGLBoolean) returns 0x3000
2023-09-04 15:36:48.786 31698-31734 Surface com.sarmadali.chatmingle D Surface::setBufferCount(this=0x74ba05f000,bufferCount=3)
2023-09-04 15:36:48.786 31698-31734 Surface com.sarmadali.chatmingle D Surface::allocateBuffers(this=0x74ba05f000)
2023-09-04 15:36:48.826 31698-31698 IInputConnectionWrapper com.sarmadali.chatmingle W requestCursorAnchorInfo on inactive InputConnection
2023-09-04 15:36:49.126 31698-31734 Surface com.sarmadali.chatmingle D Surface::disconnect(this=0x74b9dde000,api=1)
2023-09-04 15:36:49.143 31698-31698 PhoneWindow com.sarmadali.chatmingle V DecorView setVisiblity: visibility = 4, Parent = android.view.ViewRootImpl@ac646c5, this = DecorView@657b90e[SignUpActivity]
2023-09-04 15:36:49.151 31698-31698 View com.sarmadali.chatmingle D [Warning] assignParent to null: this = DecorView@657b90e[SignUpActivity]
2023-09-04 15:36:49.151 31698-31698 InputTransport com.sarmadali.chatmingle I Destroy ARC handle: 0x75106723e0
2023-09-04 15:36:49.650 31698-32110 System.out com.sarmadali.chatmingle I [socket]:check permission begin!
2023-09-04 15:36:50.491 31698-31734 Surface com.sarmadali.chatmingle D Surface::disconnect(this=0x7517854000,api=1)
2023-09-04 15:36:50.493 31698-31698 View com.sarmadali.chatmingle D [Warning] assignParent to null: this = android.widget.LinearLayout{1366271 V.E...... ......ID 0,0-371,72}
2023-09-04 15:36:50.494 31698-31698 InputTransport com.sarmadali.chatmingle I Destroy ARC handle: 0x74b9ec3400
}
3
Answers
At first, I was unable to understand the statement of @AbdullahJaved that
But After I tried to debug using below code in onCreate of SignInActivity
I have realized that the current user it is getting is the one that I have just signed up with, by matching the UserId of that user from Logcat and Firebase. So when it is being directed to SignInActivity from SignUpActivity the below code is returning true because we just have signed up and Firebase has a user against the CurrentUser, hence why it was being redirected to the MainActivity.
So technically code is correct and behaving normally according to the instructions. But unfortunately, I was not getting it Why it is happening?
When I asked ChatGpt how to prevent the new user who has just signed up from going to the main activity?
The solution to this question according to ChatGpt is to implement a user authentication and authorization system which I have not done so far. (There might be another solution that i don't know of).
Details from ChatGpt are as follows:
Authentication: Implement a user registration and login system. You can use Firebase Authentication or your own backend server for this purpose. Ensure that users can only access the app's main functionality after they have successfully registered and logged in.
User Profile Management: When a new user signs up, store their user information in a user profile on your backend server or in a local database. You may also want to include a flag or attribute in the user profile to indicate whether the user is a new user or has completed some initial setup.
MainActivity Logic: In your MainActivity, check whether the user is authenticated and authorized to access the main functionality. You can do this by verifying the user's login status and checking their user profile attributes. If the user is not authorized (e.g., because they are a new user), you can redirect them to another activity, such as an onboarding activity or a profile setup activity.
Please try to remove if(task.isSuccessful()) phrase.
So it will be like this
I have modified your code. Navigating and finishing activity once all the execution done.