简体   繁体   中英

Why i didn't get Logout Button in Next Activity in Facebook Integration with sdk 4.0.1

I Login Successfully with Facebook Sdk 4.0.1 and goto second activity which is logout activity i click on logout button and redirected again to Main activity but actually i got there again Logout button which is my Login button which change to Logout by Facebook Sdk. So please tell me what's wrong with me. Please tell me how i get that Logout Button in my Next Activity

 public class MainActivity extends FragmentActivity{ LoginButton loginButton; FacebookSdk mFacebook; private static final String[] PERMISSIONS = new String[] {"public_profile", "user_photos", "read_stream", "email" }; CallbackManager callbackManager; SharedPreferences preferences; String userName, emailId, userFacebookId, userId; URL image_value; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); FacebookSdk.sdkInitialize(getApplicationContext()); callbackManager = CallbackManager.Factory.create(); setContentView(R.layout.activity_main); System.out.println("Inside onCreate "); preferences = getSharedPreferences("MyData", Context.MODE_WORLD_WRITEABLE); loginButton = (LoginButton) findViewById(R.id.login_button); loginButton.setReadPermissions(PERMISSIONS); // If using in a fragment // loginButton.setFragment(); // Callback registration LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { @Override public void onSuccess(LoginResult loginResult) { SharedPreferences.Editor editor = preferences.edit(); editor.putString("accessToken", loginResult.getAccessToken().toString()); editor.apply(); GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() { @Override public void onCompleted(JSONObject jsonObject, GraphResponse graphResponse) { try { System.out.println("Inside onSuccess "); userFacebookId = graphResponse.getJSONObject().getString("id"); userName = graphResponse.getJSONObject().getString("name"); emailId = graphResponse.getJSONObject().getString("email"); userId = graphResponse.getJSONObject().getString("link"); try { image_value = new URL("http://graph.facebook.com/" + userFacebookId + "/picture?type=large"); } catch (MalformedURLException e) { e.printStackTrace(); } SharedPreferences.Editor editor = preferences.edit(); editor.putString("userFacebookId", userFacebookId); editor.putString("userName", userName); editor.putString("emailId", emailId); editor.putString("userId", userId); editor.putString("image_value", image_value.toString()); editor.apply(); Intent intent = new Intent(MainActivity.this, logOut.class); startActivity(intent); } catch (org.json.JSONException e) { e.printStackTrace(); } } }); request.executeAsync(); } @Override public void onCancel() { System.out.println("Inside onCancel "); // App code } @Override public void onError(FacebookException exception) { System.out.println("Inside Error " + exception); // App code } }); } @Override public void onBackPressed() { super.onBackPressed(); finish(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); System.out.println("Inside onActivityResult "+resultCode); } } 

In second activity logout method,use following line to ask the LoginManager to logout as

LoginManager.getInstance().logOut();

So it will show "Login" text on facebook login button in your Main activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM