4
I’m new in the world Android and I’m in need of some help.
I can’t make the call to a new Activity after the facebook login.
The login is running correctly and when it is successfully performed, it returns the same Activity with the button to Logout. I tried to make the call by Intent
, but it didn’t work. Could you help me, please.
I’m using the facebook sdk 4.0
Below is the code of main Ragment:
public class MainFragment extends Fragment {
private CallbackManager callbackManager;
private TextView textView;
private AccessTokenTracker accessTokenTracker;
private ProfileTracker profileTracker;
private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
AccessToken accessToken = loginResult.getAccessToken();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
public MainFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
callbackManager = CallbackManager.Factory.create();
accessTokenTracker= new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {
}
};
profileTracker = new ProfileTracker() {
@Override
protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
displayMessage(newProfile);
}
};
accessTokenTracker.startTracking();
profileTracker.startTracking();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_main, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
textView = (TextView) view.findViewById(R.id.textView);
loginButton.setReadPermissions("user_friends");
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, callback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
Intent intent = new Intent();
}
private void displayMessage(Profile profile){
if(profile != null){
textView.setText(profile.getName());
}
}
@Override
public void onStop() {
super.onStop();
accessTokenTracker.stopTracking();
profileTracker.stopTracking();
}
@Override
public void onResume() {
super.onResume();
Profile profile = Profile.getCurrentProfile();
displayMessage(profile);
}
}
Hi, Androiderson, I’ve tried it this way and I get an error: Error:(40, 13) error: method startActivity in class Fragment cannot be Applied to Given types; required: Intent found: Context,Class<Stationfragment> Reason: actual and formal argument lists differ in length
– Ari Ferreira Junior
Are you sure that your
NovaActivity.class
is aActivity
and not aFragment
?– Androiderson
Now I’ve arranged for the main Activity, but when it returns it gets the logout button on the screen... the code looks like this: public void onSuccess(Loginresult loginResult) { Accesstoken accessToken = loginResult.getAccessToken(); Profile profile profile = Profile.getCurrentProfile(); displayMessage(profile); Intent Intent = new Intent(getcontext(), Stationactivity.class); startActivity(Intent);
– Ari Ferreira Junior
and when starting the app again it drops to the logout screen and not to the main Activity
– Ari Ferreira Junior
Every time the user lands on the login screen, and the user is already logged in, you have to redirect to the correct screen. This is not automatic, you have to implement.
– Androiderson
In this case I should use the Lifecycle from facebook sdk itself?
– Ari Ferreira Junior
No, facebook only tells you whether or not the user is logged in, the rest is up to you.
– Androiderson