问题
I implemented the login via facebook for my app using Facebook SDK 4.0. When the user clicks on facebook login button he navigates to home activity and a new view is loaded. The problem is that for a while, after the facebook process dialog disappears and before main view is shown, the facebook login button changes his text in 'Log out' and this is visible by the user. How can I avoid this?
This is my code in the fragment login:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.frg_login, container, false);
callbackManager = CallbackManager.Factory.create();
fbLoginButton = (LoginButton) view.findViewById(R.id.fb_login_button);
fbLoginButton.setReadPermissions("user_friends");
fbLoginButton.setReadPermissions("public_profile");
fbLoginButton.setFragment(this);
fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
// Application code
Log.v("LoginActivity", response.toString());
session = new SessionManager(getActivity());
try{ String name = object.getString("name");
session.createLoginSession(name, true);
//Toast.makeText(getActivity(), "Login successful", Toast.LENGTH_SHORT).show();
}
catch (JSONException exe) { }
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
Toast.makeText(getActivity(), "Login canceled", Toast.LENGTH_SHORT).show();
}
@Override
public void onError(FacebookException exception) {
Toast.makeText(getActivity(), "Login error", Toast.LENGTH_SHORT).show();
}
});
return view;
}
And in the activity that calls the fragment:
if (currentAccessToken != null) {
navigatetoHomeActivity();
}
public void navigatetoHomeActivity(){
Intent homeIntent = new Intent(getActivity(),MainActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
Thanks to all
回答1:
You can change the text of your login and logout buttons to be the same by adding the following three lines to your Facebook button XML:
<com.facebook.login.widget.LoginButton
...
...
xmlns:facebook="http://schemas.android.com/apk/res-auto"
facebook:com_facebook_login_text="LOGIN"
facebook:com_facebook_logout_text="LOGIN"/>
回答2:
That's the default behaviour of the Facebook LoginButton. You could use LoginManager class instead.
The methods listed below specifically, depending on your needs:
logInWithReadPermissions(Fragment, Collection)
logInWithReadPermissions(Activity, Collection)
logInWithPublishPermissions(Fragment, Collection)
logInWithPublishPermissions(Activity, Collection)
Reference: https://developers.facebook.com/docs/reference/android/current/class/LoginManager/#logInWithReadPermissions
回答3:
I solved this by putting an invisible TextView next to the
<com.facebook.login.widget.LoginButton ...
>
in my activity_login.xml. This TextView has the same background, dimensions and other properties as the facebook button. In my case:
<TextView
android:paddingTop="13sp"
android:paddingBottom="13sp"
android:textSize="20sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/com_facebook_blue"
android:textColor="@color/white"
android:text="@string/logging_in"
android:textAlignment="center"
android:id="@+id/textviewId"
android:gravity="center"
android:textStyle="bold"
android:visibility="invisible"
android:layout_marginBottom="35dp"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
In the onCreateView method of the fragment, add an onClickListener in which you set the visibility of the LoginButton to false and the visibility of your TextView clone to true with:
fbLoginButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
fbLoginButton.setVisibility(View.INVISIBLE);
findViewById(R.id.textviewId).setVisibility(View.VISIBILE);
}
});
Finally, within your FacebookCallback<LoginResult> method you can reset the fbLoginButton visibility to View.VISIBLE and hide the TextView if the login was a failure.
Remember to reset the visibility upon fragment creation!
来源:https://stackoverflow.com/questions/30692336/facebook-login-button-changes-its-text-in-log-out-before-new-view-is-shown