Skip to content

Commit cf3a6ea

Browse files
SUPERCILEXsamtstern
authored andcommitted
Permanently fix NPEs when attempting to finish Smart Lock fragments (#618)
1 parent 6e388b3 commit cf3a6ea

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

auth/src/main/java/com/firebase/ui/auth/util/signincontainer/SmartLockBase.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package com.firebase.ui.auth.util.signincontainer;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import android.support.annotation.NonNull;
56
import android.support.annotation.Nullable;
67
import android.text.TextUtils;
78
import android.util.Log;
9+
import android.util.Pair;
810
import android.widget.Toast;
911

1012
import com.firebase.ui.auth.ui.FragmentBase;
@@ -32,7 +34,9 @@ public abstract class SmartLockBase<R extends Result> extends FragmentBase imple
3234
private static final String TAG = "SmartLockBase";
3335

3436
protected GoogleApiClient mGoogleApiClient;
37+
3538
private boolean mWasProgressDialogShowing;
39+
private Pair<Integer, Intent> mActivityResultPair;
3640

3741
/**
3842
* Translate a Firebase Auth provider ID (such as {@link GoogleAuthProvider#PROVIDER_ID}) to
@@ -110,7 +114,9 @@ public void onCreate(@Nullable Bundle savedInstanceState) {
110114
@Override
111115
public void onStart() {
112116
super.onStart();
113-
if (mWasProgressDialogShowing) {
117+
if (mActivityResultPair != null) {
118+
mHelper.finish(mActivityResultPair.first, mActivityResultPair.second);
119+
} else if (mWasProgressDialogShowing) {
114120
mHelper.showLoadingDialog(com.firebase.ui.auth.R.string.progress_dialog_loading);
115121
mWasProgressDialogShowing = false;
116122
}
@@ -135,6 +141,18 @@ public void cleanup() {
135141
}
136142
}
137143

144+
@Override
145+
public void finish(int resultCode, Intent resultIntent) {
146+
if (getActivity() == null) {
147+
// Because this fragment lives beyond the activity lifecycle, Fragment#getActivity()
148+
// might return null and we'll throw a NPE. To get around this, we wait until the
149+
// activity comes back to life in onStart and we finish it there.
150+
mActivityResultPair = new Pair<>(resultCode, resultIntent);
151+
} else {
152+
super.finish(resultCode, resultIntent);
153+
}
154+
}
155+
138156
@Override
139157
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
140158
Toast.makeText(getContext(),

0 commit comments

Comments
 (0)