0

In SelectionFragment, user.getId() gets the current logged in user's Facebook ID.

In MainActivity.java, I have an intent to start another activity.

I'm trying to figure out how to use the data from user.getId() in an intent in MainActivity.java:

Intent i = new Intent(getApplicationContext(), AllProductsActivity.class);
i.putExtra("EXTRA_FACEBOOK_ID","FB_ID_VALUE_GOES_HERE");
startActivity(i);

I've tried a lot of things on stack overflow and none of them have worked so far. Any help appreciated!!

Relevant code from SelectionFragment:

Facebook ID is collected:

private void makeMeRequest(final Session session) {
    // Make an API call to get user data and define a 
    // new callback to handle the response.
    Request request = Request.newMeRequest(session, 
            new Request.GraphUserCallback() {
        @Override
        public void onCompleted(GraphUser user, Response response) {
            // If the response is successful
            if (session == Session.getActiveSession()) {
                if (user != null) {
                    // Set the id for the ProfilePictureView
                    // view that in turn displays the profile picture.
                    profilePictureView.setProfileId(user.getId());
                    // Set the Textview's text to the user's name.
                    userNameView.setText(user.getName());

                 // Set the Textview's text to the user's name.
                    userIdView.setText(user.getId());

                }
            }
            if (response.getError() != null) {
                // Handle errors, will do so later.
            }
        }
    });
    request.executeAsync();
} 

-

private TextView userIdView;

-

userIdView = (TextView) view.findViewById(R.id.selection_user_id);

-

userIdView.setText(user.getId());

Relevant code from MainActivity:

private static final int SPLASH = 0;
private static final int SELECTION = 1;
private static final int SETTINGS = 2;
private static final int FRAGMENT_COUNT = SETTINGS +1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

    FragmentManager fm = getSupportFragmentManager();
    fragments[SPLASH] = fm.findFragmentById(R.id.splashFragment);
    fragments[SELECTION] = fm.findFragmentById(R.id.selectionFragment);
    fragments[SETTINGS] = fm.findFragmentById(R.id.userSettingsFragment);

}

EDIT

After checking out the google documentation, I added the following to my MainActivity:

SelectionFragment fragment = (SelectionFragment) getSupportFragmentManager().findFragmentById(R.id.selectionFragment);

My understanding is that this allows me to connect to the fragment, but I can't figure out how to get the facebook id from the fragment now. I tried this, but the app crashes. I get a null pointer exception at this line:

String fbID = fragment.userIdView.getText().toString();
pkelly
  • 203
  • 2
  • 3
  • 15
  • 1
    http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity – tyczj May 19 '14 at 18:18
  • This resource is very helpful. I think I'm moving in the right direction, but my app crashes with the new code I added. I just edited my post with new information at the bottom. – pkelly May 20 '14 at 01:07

1 Answers1

0

If you want to pass data from your Fragment back to its holding Activity you can use an event bus like Otto. Then you can start the new Activity from the @Subscribe method you defined to receive the event. I prefer using an event bus rather than the interface approach because components would truly be loosely coupled.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53