I was following a tutorial on this exact question but the method used (Facebooksdk.sdkInitialize) appears to be deprecated. So now I'm unsure how to open a new activity after login. Here's my code:
public class MainActivity extends AppCompatActivity {
LoginButton loginButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
What's the most simple way to open a new activity after login?
Try this ,login button have callbacks and onSuccess you can move to next activity....
//Declare this on Top
CallbackManager mCallbackManager;
LoginButton loginButton;
//in onCreate
mCallbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.button_facebook_login);
loginButton.setReadPermissions("email", "public_profile");
loginButton.registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d(TAG, "facebook:onSuccess:" + loginResult);
//OPEN ACTIVITY FROM HERE
startActivity(new Intent(CurrentActivity.this,DestinationActivity.class));
}
#Override
public void onCancel() {
Log.d(TAG, "facebook:onCancel");
// ...
}
#Override
public void onError(FacebookException error) {
Log.d(TAG, "facebook:onError", error);
// ...
}
});
//and put this method on your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
Related
I am working on a custom button which enable Facebook login. What I am coding gave me a result which I need to tap the button twice only it will start the activity.
I was trying to work on it through adjusting the position of startActivity() function in different way. And I found that I can make it to become initialization through placing the startActivity() function just after the finding of view for button bLogin.
Logically, it seems not right as no user will start to proceed to the next activity before login.
Any solution on this? Thank you in advance.
For your information, here are my MainActivity java code.
public class MainActivity extends AppCompatActivity {
CallbackManager callbackManager;
GraphRequest graphRequest;
com.facebook.login.LoginManager fbLoginManager;
Button bLogin;
#Override
protected void onActivityResult (int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//new activity from login page
public void chatbox (View view){
final Intent intent = new Intent(this, tabviewChatbox.class);
fbLoginManager = com.facebook.login.LoginManager.getInstance();
callbackManager = CallbackManager.Factory.create();
bLogin = (Button) findViewById(R.id.bLogin);
//only start activity if login success
LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
graphRequest(loginResult.getAccessToken());
startActivity(intent);
finish();
}
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException error) {
}
});
bLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LoginManager.getInstance().logInWithReadPermissions(MainActivity.this, Arrays.asList("public_profile", "email", "user_birthday", "user_friends"));
}
});
}
public void graphRequest(AccessToken accessToken) {
graphRequest = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
#Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("response", response.toString());
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,email,birthday,friends");
graphRequest.setParameters(parameters);
graphRequest.executeAsync();
}
//Function calling for integration of font awesome, can directly put icon inside default button by typing name
#Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(IconicsContextWrapper.wrap(newBase));
}
}
I am working on an app which has Facebook login functionality. I am using Facebook login. I would like to have Facebook CallBack in different classes, so I can get retrieve facebook ID and image etc.. But how do I do this? This is my LoginActivity class:
public class LoginActivity extends AppCompatActivity {
LoginButton loginButton;
TextView textView;
CallbackManager callbackManager;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_login);
loginButton = (LoginButton)findViewById(R.id.fb_login_btn);
textView = (TextView) findViewById(R.id.textView);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Intent myIntent = new Intent(LoginActivity.this,MainMenu.class);
startActivity (myIntent);
finish(); //if you want to do not use this
}
#Override
public void onCancel() {
textView.setText("Login Cancelled");
}
#Override
public void onError(FacebookException error) {
textView.setText("Login Failed");
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
}
}
and I have a Profile Class, where I want to access Facebook Profile Picture and email address. Can anyone help?
Probably if you inherit FacebookCallback like:
ProfileClass extends FacebookCallback
Then pass an instance of ProfileClass into:
loginButton.registerCallback(callbackManager, profileClass);
In your ProfileClass implement onSuccess(), onCancel() and onError() methods and write your desired logic in them.
I used the following code snippet in my Android app to ask user to login to Facebook
public class FacebookLoginActivity extends AppCompatActivity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
initCallback();
LoginManager.getInstance().logInWithReadPermissions(this, FacebookUtils.requiredPermissions);
}
private void initCallback() {
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
AccessToken.setCurrentAccessToken(loginResult.getAccessToken());
#Override
public void onCancel() {
}
#Override
public void onError(FacebookException exception) {
}
}
);
}
#Override
protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
However, the methods defined in the callback never gets called. Anyone can help me?
I have an activity for user to login with facebook. I used facebook-android-sdk v4.0.0. But login callback is not triggered when user click on login button. After showing progress bar, start previous activity automatically without showing any error on log instead of triggering login callback.
In SignUpActivity,
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
callbackManager = CallbackManager.Factory.create();
LoginButton loginButton = (LoginButton) findViewById(R.id.login_btn);
loginButton.setReadPermissions(Arrays.asList("user_friends", "email"));
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.i("Login : ", "Success");
}
#Override
public void onCancel() {
Log.i("Login : ", "Cancel");
}
#Override
public void onError(FacebookException e) {
Log.e("Login Error : ", e.getMessage() + "");
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I also added meta-data element to application element and the FacebookActivity to the manifest. Moreover, finished initializing facebook SDK in Application :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
}
What's wrong with my code ? Thanks for any suggestion !!
Edit
I found myself what's wrong with my code. I start SignUpActivity with no_history flag. After I remove that flag, everything is okay now.
Error:(36, 20) error: cannot find symbol method
registerCallback(CallbackManager,>)
When i called this registerCallback from my Mainactivity to facebook sdk 4.0's lib. Here is my class
public class MainActivity extends Activity {
TextView tv;
Button loginButton;
CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("Oncreate", "Now on On create");
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
tv =(TextView)findViewById(R.id.tv);
loginButton = (LoginButton)findViewById(R.id.login_button);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
// App code
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
}
You must call FacebookSdk.sdkInitialize(this.getApplicationContext()) before setContentView(). sample code:
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.loginFaceBook_button);
//etc..
Also the documentation mentions to either use loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() or LoginManager.getInstance().registerCallback (callbackManager, new FacebookCallback<LoginResult>() , not both of them. Here is a full sample code:
private LoginButton loginButton;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(this.getApplicationContext());
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
loginButton = (LoginButton) findViewById(R.id.loginFaceBook_button);
List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "public_profile");
loginButton.setReadPermissions(permissionNeeds);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
{
#Override
public void onSuccess(LoginResult loginResult)
{
System.out.println("onSuccess");
}
#Override
public void onCancel()
{
System.out.println("onCancel");
}
#Override
public void onError(FacebookException exception)
{
Log.v("LoginActivity", exception.getCause().toString());
}
});
Don't forget to include onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
loginButton must be of type LoginButton instead of Button.
Unrelated: You do not need to register the callback on both the loginButton and LoginManager.getInstance(). The former is for the loginButton flow, the latter if you have your own button or login trigger. In fact it's not clear on Facebook's documentation, not your fault.