1

I have an activity with 2 fragments, one handles all data and other is only like a scoreboard.

Fragment1 asks for a new player name when one player is out, and fragment2 will use the name for the scoreboard. I'm using a static method in fragment1 getPlayerName() and using it in fragment2.

The app consists of a ListView + custom ArrayAdapter combo to populate the fragment2.

Here's the simplified code with required details, it doesn't show errors but doesn't work. I press out, enter the new name, but it doesn't show up in the Scoreboard.

Tried notifydatasetchanged() at various places but seems like that isn't the solution.

FieldFragment.java

public class FieldFragment extends Fragment {

static String playerNames[] = new String[11];
    int i = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View rootView = inflater.inflate(R.layout.fragment_field, container, false);

        Button out = (Button)rootView.findViewById(R.id.out);
        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                displayDialog();
            }
        });

        return rootView;


    }

public void displayDialog(){
        final Dialog nameDialog = new Dialog(getActivity());
        nameDialog.setContentView(R.layout.dialog);

        Button ok = (Button)nameDialog.findViewById(R.id.ok_button);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText nameET = (EditText)nameDialog.findViewById(R.id.name_edit_text);
                playerNames[i] = nameET.getText().toString();
                nameDialog.dismiss();
            }
        });


        Button cancel = (Button)nameDialog.findViewById(R.id.cancel_button);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nameDialog.dismiss();
            }
        });
        nameDialog.show();
    }

    public static String getPlayerName(int playerNumber) {

        return playerNames[playerNumber];
    }
}

And ScoreboardFragment:

public class ScoreboardFragment extends Fragment {

    public ScoreboardFragment() {
        // Required empty public constructor
    }
    PlayerAdapter playerAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_scoreboard, container, false);

        int i=0;

        ArrayList<Players> players = new ArrayList<>();
        players.add(new Players(getPlayerName(i++),0,0));
        players.add(new Players(getPlayerName(i++),0,0));

        playerAdapter = new PlayerAdapter(getActivity(), players);
        ListView playerList = (ListView)rootView.findViewById(R.id.list_of_players);
        playerList.setAdapter(playerAdapter);

        return rootView;
    }
}

What's wrong with the code?

Let know if you need more files

Abhijeet
  • 345
  • 1
  • 4
  • 15

3 Answers3

0

A fragment is a weak controller, also avoid using static variables for passing data.

On the other side, An Activity must have a controller on all the fragments inside it.

One thing that might be your problem is that you're reinstanciating the fragments every time you present them.

A cleaner work around is putting the data inside the Activity and then configuring the data using private methods inside the activity from Fragments.

But make sure you're getting the data/activity inside the onActivityCreated() method in Fragment; otherwise the activity might be null (on some methods like onCreateView() the activity is not yet accessible)

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
Mohammed
  • 126
  • 7
0

For passing data between 2 activity with 2 fragments are:

Pass data to SecondActivity with Intent

Intent intent = new Intent(getActivity, SeconActivity.class);
intent.putExtra("Player", name);
startActivity(intent);

Receive data inside SecondActivity

String name = getIntent().getStringExtra("Player");

Send Value to ScoreboardFragment when object create

ScoreboardFragment  fragment = new ScoreboardFragment (name);

And finally you get the player name. Now you can use the player name wherever you want

0

By looking at your code there can be following issues:

  1. you are not incrementing i in FieldFragment, so after every change player name is added to 0th location.

  2. As you are showing some dialog to get player name if ScoreboardFragment is already created onResume() will be called.

So please check if onCreateView() is getting called to update name. For more details check onCreateView method gets called when? and How many times in Activity life cycle?

Tom
  • 1,387
  • 3
  • 19
  • 30
vishakha yeolekar
  • 3,294
  • 1
  • 9
  • 9
  • 1. Well, made the i global. 2. I don't know how that will matter, but anyway, I tried onResume(), doesn't help – Abhijeet Jul 23 '17 at 15:35
  • You can use callback mechanism for passing data between fragments. For more info: http://www.journaldev.com/14207/android-passing-data-between-fragments – vishakha yeolekar Jul 24 '17 at 15:44