简体   繁体   English

使用Android应用程序登录Facebook SDK 4.0.1

[英]login facebook sdk 4.0.1 with android application

I'm developing an android application that user can connect with Facebook. 我正在开发一个可以与Facebook连接的android应用程序。 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 将数据保存到outState Bundle in

void onSaveInstanceState(Bundle outState)

and restore it from savedInstanceState in 并从savedInstanceState中还原它

void onCreate(Bundle savedInstanceState)

of your fragment. 你的片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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