0

So i have a FragmentPagerAdapater called SectionsPagerAdapter and a fragment called TeamFragment where I display data from a specific team. So basically I don't want to create different fragments for each team. That is an overkill. I just want 1 fragment which basically connects to the backend then collects the data based on the team then displays that data. But I dont know how to pass the Team name(a string type) from SectionsPagerAdapter to the TeamFragment so that in TeamFragment, I can easily know what to retrieve from the backend. My backend in parse.com. Please help me figure this out and learn. Thanks

2 Answers 2

1

So this is was solved my problem. In my sectionsPagerAdapter class i had the below code

    Bundle args = new Bundle();
    args.putString("TeamName", team);
    TeamFragment teamFragment = new TeamFragment();
    teamFragment.setArguments(args);

In onCreateView of my TeamFragment, i had the following

Bundle bundle = this.getArguments();
    mTeam = bundle.getString("TeamName");

hope this can help someone else. Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

Communicating data into fragments is typically done through a simple setter function that is called by the activity that instantiates or contains the fragment:

public class MyActivity extends FragmentActivity {
   @Override
   protected void onCreate(Bundled savedInstanceState) {
     // ...
     TeamFragment fragment =
         (TeamFragment) (getSupportFragmentManager().findFragmentById(fragmentId));
     fragment.setTeamName(teamName);
     // ...
}

For communicating data back to the activity, is typically done using a fragment-specific "Listener" interface. This listener can be attached using the same method (by calling a method on the fragment in the parent activity to register the listener) or it can be done by requiring that the parent Activity implement the listener interface, and casting the parent activity to this listener interface in onAttach() (though the latter approach is not as clean of an approach). Example:

public class MyActivity extends FragmentActivity {
   @Override
   protected void onCreate(Bundled savedInstanceState) {
     // ...
     TeamFragment fragment =
         (TeamFragment) (getSupportFragmentManager().findFragmentById(fragmentId));
     fragment.setTeamName(teamName);
     fragment.setTeamSelectedListener(new TeamSelectedListenerImpl());
     // ...
}

Or:

public class TeamFragment extends Fragment {
   public interface TeamSelectedListener {
      // ...
   }

   // ...
   @Override
   protected void onAttach(Activity activity) {
      teamSelectedListener = (TeamSelectedListener) activity;
   }
   // ...
}

public class MyActivity
    extends FragmentActivity
    implements TeamFragment.TeamSelectedListener {
    // ...
}

1 Comment

Hey thanks for your suggestion, but i didnt try it to see if it solved my pb. I post my answer tho. Thanks once more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.