修改:问题图片,http://imgur.com/a/N4pRb
使用Facebook登录时,我会弹出提示用户登录,这会将我带到一个“确认登录”页面,上面写着我的应用程序已被授权。我单击确定,它会将我带回初始登录弹出窗口。当我单击左上角的“x”时,我的onSuccess()代码运行。登录完成后,onSuccess()方法应处理登录并将用户带到另一个活动。我从哪里开始?
MainActivity,
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.content_main);
fragment.onActivityResult(requestCode, resultCode, data);
}
LoginFragment,
public class LoginFragment extends MvpFragment<LoginView, LoginPresenter> implements LoginView, View.OnClickListener {
private EditText _userName;
private EditText _password;
private Button _loginWithFacebook;
private CallbackManager _callbackManager;
private static final String TAG = LoginFragment.class.getSimpleName();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_login, container, false);
view.findViewById(R.id.login_btn).setOnClickListener(this);
view.findViewById(R.id.login_forgot_password).setOnClickListener(this);
view.findViewById(R.id.login_signup).setOnClickListener(this);
view.findViewById(R.id.login_skip).setOnClickListener(this);
_userName = (EditText) view.findViewById(R.id.login_username);
_password = (EditText) view.findViewById(R.id.login_password);
_loginWithFacebook = (Button) view.findViewById(R.id.facebook_login_button);
_loginWithFacebook.setOnClickListener(this);
LoginManager.getInstance().logOut();
return view;
}
@NonNull
@Override
public LoginPresenter createPresenter() {
return new LoginPresenterImpl((LoginRouter) getActivity(), new PreferencesManager(getContext()));
}
@Override
public void authenticationError() {
AlertDialogFragment.newInstance(R.string.login_authentication_error, false).show(getChildFragmentManager(), null);
}
@Override
public void onShowEmptyFieldError() {
AlertDialogFragment.newInstance(R.string.login_empty_field_error, false).show(getChildFragmentManager(), null);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.facebook_login_button:
//presenter.onFacebookLoginClick();
facebookLogIn();
break;
case R.id.login_btn:
doLogin();
break;
case R.id.login_forgot_password:
presenter.onForgotPasswordClick();
break;
case R.id.login_signup:
presenter.onSignUpClick();
break;
case R.id.login_skip:
presenter.onSkipForNowClick();
break;
}
}
private void doLogin() {
presenter.onLocalLoginClick(_userName.getText().toString().trim(),
_password.getText().toString().trim());
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (_callbackManager != null) {
_callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void facebookLogIn() {
_callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(_callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "Successful Login");
GraphRequestBatch batch = new GraphRequestBatch(
GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject jsonObject,
GraphResponse response) {
// Application code for user
try {
Log.d(TAG, jsonObject.toString());
String s = jsonObject.getString("name");
presenter.checkFacebookUserExists(s);
} catch (Exception e) {
Log.d(TAG, "Login Unsuccesful");
}
}
})
);
batch.executeAsync();
}
@Override
public void onCancel() {
// do nothing
}
@Override
public void onError(FacebookException exception) {
Log.d(TAG, exception.getMessage());
//showNetworkError();
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
}
}
清单,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.some.placeholder">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name=".SomeApplication"
android:allowBackup="true"
android:hardwareAccelerated="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".SplashActivity"
android:theme="@style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="sensorPortrait">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".login.LoginActivity"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity android:name=".FireworksAnimationActivity" />
<service
android:name=".activecrumble.crumblefood.BackgroundStepTracker"
android:exported="false"
android:stopWithTask="true">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE" />
</intent-filter>
</service>
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id" />
</application>
答案 0 :(得分:0)
通过在我的布局中使用常规按钮而不是Facebook LoginButton来解决它。