我想在开始新意图时破坏活动
I want to destroy activities on starting a new intent
我有两个活动 MainActivity 和 signUp。 MainActivity 在应用程序启动时创建,它获取 firebase 身份验证实例,如果未找到用户,则转到注册。
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);
}
}
现在,当新用户在 signUp 中注册时,它会转到主屏幕 activity 但我面临的问题是,当新用户注册并从 MainActivity 开始意图时,authStateListener 正在获取用户。
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();
}
});
}
});
}
}
这是来自主屏幕的日志 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..
这里它在注册时通知 authStateListener,在一些代码之后它从注册中获取意图 class。我希望它销毁 MainActivity 并直接从 signUp 获取意图而不通知 authStateListener。
请帮忙。
你可以像这样摧毁一个activity:
第 1 步:在 MainActivity
中声明一个 Activity
变量
public static Activity MainActivityContext;
第 2 步:在 OnCreate
方法 MainActivity
中初始化 MainActivityContext
变量
MaMainActivityContext = this;
第 3 步:现在转到要销毁 MainActivity
的 java 语句并写入
MainActivity.MainActivityContext.finish();
MainActivity
始终启动 signup
Activity
。在用户已经存在的情况下,HomeScreen
Activity
也会启动。这可能不是你想要的。
此外,authListener
回调调用 MainActivity.this.finish()
但此时 MainActivity
已经完成,所以这也是错误的。
如果您不希望 authListener
在用户注册时触发,您应该在创建用户之前删除侦听器。
总的来说,您的代码可以改进。您应该查看更多教程,以更好地了解如何执行此操作。
我有两个活动 MainActivity 和 signUp。 MainActivity 在应用程序启动时创建,它获取 firebase 身份验证实例,如果未找到用户,则转到注册。
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);
}
}
现在,当新用户在 signUp 中注册时,它会转到主屏幕 activity 但我面临的问题是,当新用户注册并从 MainActivity 开始意图时,authStateListener 正在获取用户。
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();
}
});
}
});
}
}
这是来自主屏幕的日志 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..
这里它在注册时通知 authStateListener,在一些代码之后它从注册中获取意图 class。我希望它销毁 MainActivity 并直接从 signUp 获取意图而不通知 authStateListener。
请帮忙。
你可以像这样摧毁一个activity:
第 1 步:在 MainActivity
中声明一个Activity
变量
public static Activity MainActivityContext;
第 2 步:在 OnCreate
方法 MainActivity
MainActivityContext
变量
MaMainActivityContext = this;
第 3 步:现在转到要销毁 MainActivity
的 java 语句并写入
MainActivity.MainActivityContext.finish();
MainActivity
始终启动 signup
Activity
。在用户已经存在的情况下,HomeScreen
Activity
也会启动。这可能不是你想要的。
此外,authListener
回调调用 MainActivity.this.finish()
但此时 MainActivity
已经完成,所以这也是错误的。
如果您不希望 authListener
在用户注册时触发,您应该在创建用户之前删除侦听器。
总的来说,您的代码可以改进。您应该查看更多教程,以更好地了解如何执行此操作。