简体   繁体   中英

login facebook sdk 4.0.1 with android application

I'm developing an android application that user can connect with Facebook. I can login and get the profile information but when I pass to another fragment and back to login fragment the user remind connected but I can't see the profile information. How to keep these information visible when I pass or change the fragment?

This is my code:

public class Share extends Fragment {

LoginButton loginButton;
TextView nameT,emailT,locationT,idT,msginfo;
ProfilePictureView ppv;
CallbackManager callbackManager;
private ShareDialog shareDialog;
File destination;
String imagePath;
Bitmap bmp;
ImageView takePhoto,preview;
private static final int REQUEST_IMAGE = 100;

RelativeLayout otherview;
AccessTokenTracker accessTokenTracker;

private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
@Override
public void onActivityResult(int requestCode, int responseCode, Intent data)
{
    super.onActivityResult(requestCode, responseCode, data);
    callbackManager.onActivityResult(requestCode, responseCode, data);

}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getActivity().getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
    shareDialog = new ShareDialog(this);

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.share, container, false);

    super.onCreate(savedInstanceState);

    loginButton = (LoginButton)view.findViewById(R.id.authButton);
    nameT= (TextView)view.findViewById(R.id.name);
    locationT= (TextView)view.findViewById(R.id.location);
    emailT= (TextView)view.findViewById(R.id.mail);
    idT=(TextView)view.findViewById(R.id.id);
     ppv = (ProfilePictureView)view.findViewById(R.id.fbimg);
    otherview=(RelativeLayout)view.findViewById(R.id.other_views);
    msginfo=(TextView)view.findViewById(R.id.msginfo);






    List<String> permissionNeeds = Arrays.asList("user_photos", "email", "user_birthday", "user_location", "public_profile");

    loginButton.setReadPermissions(permissionNeeds);
   loginButton.setFragment(this);


    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
    {
        @Override
        public void onSuccess(LoginResult loginResult)
        {
            System.out.println("onSuccess");
            msginfo.setText("You can now share image on facebook");
            otherview.setVisibility(View.VISIBLE);

            GraphRequest request = GraphRequest.newMeRequest
                    (loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
                    {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response)
                        {
                            // Application code
                            Log.v("LoginActivity", response.toString());
                            //System.out.println("Check: " + response.toString());
                            try
                            {
                                String id = object.getString("id");
                                idT.setText(object.getString("id"));
                                ppv.setProfileId(object.getString("id"));
                                nameT.setText(object.getString("name"));
                                emailT.setText(object.getString("email"));
                                locationT.setText(object.getString("address"));

                                String name = object.getString("name");
                                String email = object.getString("email");
                                String gender = object.getString("gender");
                                String birthday = object.getString("birthday");
                                // String location = object.getString("location");
                                // String location = object.getString("user_location");
                                // String location = object.getString("address");




                                System.out.println(id + ", " + name + ", " + email + ", " + gender + ", " + birthday);
                                // locationT.setText(location);

                            }
                            catch (JSONException e)
                            {
                                e.printStackTrace();
                            }

                        }
                    });
            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender, birthday,link");
            request.setParameters(parameters);
            request.executeAsync();
        }
        @Override
        public void onCancel()
        {
            System.out.println("onCancel");
        }

        @Override
        public void onError(FacebookException exception)
        {
            System.out.println("onError");
            Log.v("LoginActivity", exception.getCause().toString());
        }
    });

    return view;

}

}

Save the data to outState Bundle in

void onSaveInstanceState(Bundle outState)

and restore it from savedInstanceState in

void onCreate(Bundle savedInstanceState)

of your fragment.

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