1

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!

Ahmed Khalaf
  • 1,401
  • 2
  • 15
  • 29
Colin11
  • 141
  • 1
  • 10
  • Check [this](http://stackoverflow.com/a/5374603/715593). So basically, to transfer, do: `intent.putStringArrayListExtra`, and to get, do: `intent.getStringArrayListExtra`. – Ahmed Khalaf Jul 21 '15 at 00:53
  • It doesn't work :(.I am still getting empty result. I feel intent shouldn't be in onActivityResult since it gets called before the friendlist is generated. – Colin11 Jul 21 '15 at 01:05
  • So, the second activity is started but with no results? or not started at all? – Ahmed Khalaf Jul 21 '15 at 01:17
  • yes second activity starts first and I get a blank array list of friends and later it prints the friendlist in the first activity .So I guess second activity starts before it fetches the friendlist. – Colin11 Jul 21 '15 at 04:56

2 Answers2

2

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

Reprator
  • 2,859
  • 2
  • 32
  • 55
  • Ya i tried accessing the friend list directly but its again null. I am new to android so I really don't know how to implement the 2nd and 3rd points you have mentioned. Is it possible for you to give a small example? – Colin11 Jul 21 '15 at 04:58
  • http://www.intridea.com/blog/2011/5/24/how-to-use-application-object-of-android (point 2) – Reprator Jul 21 '15 at 05:17
  • Thanks a lot Vikram! I followed the following link http://www.intertech.com/Blog/androids-application-class/ and created an application class to access the data globally and it worked! :)) – Colin11 Jul 21 '15 at 06:59
0

To transfer

intent.putStringArrayListExtra("friendlist", friendlist).

To get

intent.getStringArrayListExtra("friendlist").

Docs: put & get.

Community
  • 1
  • 1
Ahmed Khalaf
  • 1,401
  • 2
  • 15
  • 29