I'm new in Android programming and I don't know how I can put the facebook logout botton in another activity (called whatToDo) considering that the login botton is in the LoginActivity.
This is the initial code of Facebook tutorial to implement Facebook login, but from this code I don't know how to implement what I want.
public class LoginActivity extends Activity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
TextView login_text = (TextView) findViewById(R.id.fb_login);
LoginButton fbLoginButton = (LoginButton) findViewById(R.id.fb_login_button);
fbLoginButton.setReadPermissions("email");
fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(LoginActivity.this, whatToDo.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Logged!", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Cancel Event!", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
What extra code do I have to insert in LoginActivity and whatToDo activity?
Thanks in advance!
Related
So here is the code
public void redirectToMain(){
Intent intent = new Intent(getApplicationContext(), MainScreen.class);
startActivity(intent);
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pers.clear();
pers.add("user_friends");
pers.add("email");
if (ParseUser.getCurrentUser() != null) {
redirectToMain();
}
loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions("email", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
parseLogin();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
public void parseLogin(){
loginButton.setVisibility(View.INVISIBLE);
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, pers, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if(user==null){
Log.i("Cancel", "facebook");
}else if(user.isNew()){
Log.i("Parse", "succcess");
redirectToMain();
}
else{
Log.i("Parse", "old");
redirectToMain();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data );
ParseFacebookUtils.onActivityResult(requestCode,resultCode,data);
}
}
And everything works fine, the user gets redirected to the second activity, but the activity gets called twice. Also, the log part that says old is also written twice in the logcat, so my assumption is that either parseLogin() or the ParseFacebookUtils.log...() is called twice. Any idea why that is happening?
I managed to fix it by adding an int and a while loop that executes the code only once. It's more of a hack than a fix, so maybe somebody can come up with a better solution.
Here is how I implemented callback mechanism for FB login button:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_main);
loginButton = (LoginButton)findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("FB onSuccess 1", "");
}
#Override
public void onCancel() {
Log.d("FB onCancel 1", "");
}
#Override
public void onError(FacebookException e) {
Log.d("FB onError 1", "");
}
});
}
When pressing button a spinner appear, starts, but the confirmation screen does not appear, and no log messages are filled into Activity Monitor. What is the problem?
Try Adding this in your Activity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
The facebook login is showing, I logged into facebook, confirmed the information, but when I go back to my app the AccessToken did not work. When I tested:
FacebookSdk.sdkInitialize(getApplicationContext(), new FacebookSdk.InitializeCallback() {
#Override
public void onInitialized() {
if(AccessToken.getCurrentAccessToken() == null){
System.out.println("not logged in yet");
} else {
System.out.println("Logged in");
}
}
});
the result is not logged in yet
This is the code login:
public void loginFacebook(View v){
LoginManager.getInstance().logInWithPublishPermissions(this, Arrays.asList("publish_actions"));
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(UserPerfilActivity.this, "VocĂȘ Clicou em: ", Toast.LENGTH_LONG).show();
System.out.println("publish");
// sharePhotoToFacebook();
}
#Override
public void onCancel() {
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception) {
System.out.println("onError");
}
});
}
But the callbackManager doesn't work, I don't have publish or cancel or error println.
Fix the problem, I added this code:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,
resultCode, data);
}
this is the post:
post stackoverflow link
for my app i want my facebook button fully customizable. That means for me to no use the com.facebook.login.widget.LoginButton and use a normal android button instead. To make it work i tried this solution with getting an LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));but it throws an error saying:
Cannot resolve method logInWithReadPermissions(anonymous
android.view.View.OnClickListener, java.util.List)
Can you please help?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("Success", "Login");
}
#Override
public void onCancel() {
Toast.makeText(MainActivity.this, "Login Cancel", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(MainActivity.this, exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
setContentView(R.layout.activity_main);
Button btn_fb_login = (Button)findViewById(R.id.btn_fb_login);
btn_fb_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Problem is in this line:
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends"));
Here 'this' points to the onClickListener. But it should point to 'activity' or 'fragment'.
So replace this with YourActivityName.this.
After spending days trying to figure this out, I couldn't come up with anything that worked. Using my code, it shows me the permission form which I accept and log in but it doesn't get to the onSuccess, onError or onCancel. Anytime I click the button, it just doesn't do anything. And no errors on the LogCat. I don't know where i'm going wrong.
fb = (Button) findViewById(R.id.fb_button);
fb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("kkkkkk","kkllkl");
}
#Override
public void onCancel() {
Log.d("kkkkkk","kkllkl2");
}
#Override
public void onError(FacebookException error) {
Log.d("kkkkkk","kkllkl4");
}
});
}
});
The FacebookSdk.sdkInitialize(this.getApplicationContext()); is initialized after the super.onCreate(savedInstanceState);
I finally figured it out. I changed the code a bit from the initial code to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("kkkkkk","kkllkl");
}
#Override
public void onCancel() {
Log.d("kkkkkk","kkllkl2");
}
#Override
public void onError(FacebookException error) {
Log.d("kkkkkk","kkllkl4");
}
});
and then in the button's on click listener, i just had to make the call for the login.
fb.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
}
});
also don't forget to add the onActivityResult. I made the mistake also.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
The complete code is below.
public class MainActivity extends ActionBarActivity {
CallbackManager callbackManager;
Button but, but2;
AccessTokenTracker accessTokenTracker;
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.button);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
// savedInstanceState
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends" ));
// }
accessTokenTracker = new AccessTokenTracker() {
#Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
}
};
but.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getUserData(AccessToken.getCurrentAccessToken());
Log.d("Access Token: ", AccessToken.getCurrentAccessToken().toString());
}
});
}
public void getUserData(AccessToken accessToken){
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
//JSONParser parser=new JSONParser();
// Data data = new Gson().fromJson(json, Data.class);
long ID = 0;
try {
String name = response.getRawResponse();
Toast.makeText(MainActivity.this, "response is: "+object.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "error is: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
}
}