简体   繁体   中英

Pass friend list and object id from graph API to another activity

I am able to fetch friend list and associated object id but I dont know how to pass these to another activity. When I use intent to pass data, I get an empty result in the next activity. Please can someone help me out.

Here is my code

 public class MainFragment extends Fragment {

        private CallbackManager callbackManager;
        private TextView textView;
        private AccessTokenTracker accessTokenTracker;
        private ProfileTracker profileTracker;
        static ArrayList<String> friendlist = new ArrayList<String>();
        public MainFragment() {

        }


        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

            callbackManager = CallbackManager.Factory.create();

          }

        @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);
            final ProfilePictureView pictureView=(ProfilePictureView) view.findViewById(R.id.picture);;

            loginButton.setFragment(this);
            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
             {
                @Override
                public void onSuccess(LoginResult loginResult) {

                    // AccessToken accessToken = loginResult.getAccessToken();
                    Toast.makeText(getActivity().getApplicationContext(),"Login Success", Toast.LENGTH_LONG).show();
                    LoginManager.getInstance().logInWithReadPermissions(getActivity(), Arrays.asList("user_friends","user_photos","email"));
                    GraphRequest request = GraphRequest.newMeRequest(
                            AccessToken.getCurrentAccessToken(),
                            new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(
                                        JSONObject object,
                                        GraphResponse response) {
                                    // Application code
                                    String name = object.optString("name");
                                    String id = object.optString("id");
                                    String email = object.optString("email");
                                    Log.d("Name",name);
                                    Log.d("ID",id);
                                    Log.d("email",email);
                                }
                            });
                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,email");
                    request.setParameters(parameters);
                    request.executeAsync();

                    GraphRequestBatch batch = new GraphRequestBatch(
                            GraphRequest.newMyFriendsRequest(
                                    AccessToken.getCurrentAccessToken(),
                                    new GraphRequest.GraphJSONArrayCallback() {
                                        @Override
                                        public void onCompleted(
                                                JSONArray jsonArray,
                                                GraphResponse response) {
                                            JSONObject object=response.getJSONObject();


                                            try {
                                                JSONArray listFriends = object.getJSONArray("data");

                                                //get the entire friendlist
                                                for(int i=0;i<listFriends.length();i++)
                                                {
                                                    JSONObject firstFriend=listFriends.getJSONObject(i);

                                                    friendlist.add(firstFriend.optString("name"));
                                                    Log.d("listOfFriends", String.valueOf(friendlist));
                                                }

                                               // String friendName=firstFriend.optString("name");
                                               // Log.d("friendname",friendName);
                                                //String friendid = firstFriend.optString("id");

                                                Log.d("listFriends", listFriends.length() + "");


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


                                        }
                                    }),
                            GraphRequest.newMyFriendsRequest(
                                    AccessToken.getCurrentAccessToken(),
                                    new GraphRequest.GraphJSONArrayCallback() {
                                        @Override
                                        public void onCompleted(
                                                JSONArray jsonArray,
                                                GraphResponse response) {
                                            // Application code for users friends
                                        }
                                    })
                    );
                    batch.addCallback(new GraphRequestBatch.Callback() {
                        @Override
                        public void onBatchCompleted(GraphRequestBatch graphRequests) {
                            // Application code for when the batch finishes
                        }
                    });
                    batch.executeAsync();



                }

                @Override
                public void onCancel() {
                    Toast.makeText(getActivity().getApplicationContext(),"Login Cancel", Toast.LENGTH_LONG).show();

                }

                @Override
                public void onError(FacebookException e) {
                    Toast.makeText(getActivity().getApplicationContext(),"Login Error", Toast.LENGTH_LONG).show();

                }

            });

        }

        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            callbackManager.onActivityResult(requestCode, resultCode, data);

                Intent secondActivityIntent = new          Intent(MainFragment.this.getActivity(), BackCamera.class);

                secondActivityIntent.putExtra("friendlist", friendlist);
                Log.d("friendlistnew", String.valueOf(friendlist));

                startActivity(secondActivityIntent);
    }
            @Override
        public void onStop() {
            super.onStop();

        }

        @Override
        public void onResume() {
            super.onResume();
            Profile profile = Profile.getCurrentProfile();

        }
    }

Thanks in advance!

There can be a multiple solutions, 1. as you had declare friend list as static, you can access it directly from anywhere

  1. Just put the friend list in application class, and get or set the data from there

3.Create a modal class implementing serializable or parceable

To transfer

intent.putStringArrayListExtra("friendlist", friendlist) .

To get

intent.getStringArrayListExtra("friendlist") .

Docs: put & get .

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