根据this帖子和其他一些博客文章,在MVP安卓应用中的演示者中使用活动上下文是不好的。如果演示者中的某个方法需要“活动”上下文,如何删除演示者中的“活动”上下文并仍然让演示者完成完成所需的工作,该怎么办?
在此Activity中,它在演示者中调用firebaseAuthWithGoogle方法。通过将活动,视图和 FirebaseAuth 传递给构造函数来初始化演示者。
public class GoogleSignInActivity extends AppCompatActivity implements
GoogleApiClient.OnConnectionFailedListener, View.OnClickListener, GoogleSignInView {
private static final String TAG = "SignInActivity";
private static final int RC_SIGN_IN = 9001;
private SignInButton mSignInButton;
private GoogleApiClient mGoogleApiClient;
// Firebase instance variables
private FirebaseAuth mFirebaseAuth;
private GoogleSignInPresenter googleSignInPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google_sign_in);
mSignInButton = (SignInButton) findViewById(R.id.btn_sign_in);
mSignInButton.setOnClickListener(this);
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
// Initialize FirebaseAuth
mFirebaseAuth = FirebaseAuth.getInstance();
googleSignInPresenter = new GoogleSignInPresenter(this, this, mFirebaseAuth);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_sign_in:
googleSignInPresenter.onSignInClick();
break;
default:
return;
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
googleSignInPresenter.firebaseAuthWithGoogle(account);
} else {
Log.e(TAG, "Google Sign-In failed.");
}
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
Log.d(TAG, "onConnectionFailed:" + connectionResult);
Toast.makeText(this, "Google Play Services error.", Toast.LENGTH_SHORT).show();
}
@Override
public void startSignInIntent() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void startMainActivity() {
startActivity(new Intent(GoogleSignInActivity.this, MainActivity.class));
finish();
}
}
此演示者中的firebaseAuthWithGoogle方法需要addOnCompleteListener中的Activity上下文。
public class GoogleSignInPresenter {
public static final String TAG = "GoogleSignInPresenter";
private GoogleSignInView googleSignInView;
private FirebaseAuth firebaseAuth;
private GoogleSignInActivity googleSignInActivity;
public GoogleSignInPresenter(GoogleSignInActivity googleSignInActivity, GoogleSignInView googleSignInView, FirebaseAuth firebaseAuth) {
this.googleSignInActivity = googleSignInActivity;
this.googleSignInView = googleSignInView;
this.firebaseAuth = firebaseAuth;
}
public void onSignInClick() {
googleSignInView.startSignInIntent();
}
public void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGooogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
firebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(googleSignInActivity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
} else {
googleSignInView.startMainActivity();
}
}
});
}
}
答案 0 :(得分:1)
我很确定你可以删除活动实例,例如
.addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
Log.d(TAG, "signInWithCredential:onComplete:" + task.isSuccessful());
if (!task.isSuccessful()) {
Log.w(TAG, "signInWithCredential", task.getException());
} else {
googleSignInView.startMainActivity();
}
}
});