0

I am refactoring an Android App to use fragments. I have a fragment which I add to the layout with transaction.replace method but whose onCreateView method is not called. Code looks as follows:

FragmentManager fragmentManager = m_Activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

m_StartNewGameFragment = new StartNewGameToggleButtonsFragment();
fragmentTransaction.replace(R.id.bottom_pane, m_StartNewGameFragment);
fragmentTransaction.commit();

String winnerText = null;
if (isComputerWinner)
    winnerText = m_Activity.getResources().getText(R.string.computer_winner).toString();
else
    winnerText = m_Activity.getResources().getText(R.string.player_winner).toString();

m_StartNewGameFragment.updateStats(computerWins, playerWins, winnerText);

The method updateStats of fragment is as follows:

public void updateStats(int computer_wins, int player_wins, String winnerText) {
     System.out.println("Update stats " + m_ComputerWins);
     m_ComputerWins.setText(Integer.toString(computer_wins));
     m_PlayerWins.setText(Integer.toString(player_wins));
     m_WinnerTextView.setText(winnerText);
}

When updateStats is called m_ComputerWins is null and the program crashes. m_ComputerWins is initialized inside the onCreateView method of the fragment which seems not to be called.

Can anyone please help ?

Cristi
  • 648
  • 1
  • 13
  • 28
  • 1
    `onCreateView` is called not inside `fragmentTransaction.commit();`, but asynchronously later. Therefore, you should not expect it to be called before `updateStats` – Vladyslav Matviienko Sep 20 '19 at 09:27
  • Welcome to Android development, where everything is asynchronous, such as view inflation. – Isak Sep 20 '19 at 10:34

3 Answers3

3

Take global variables in your fragment class for computer_wins, player_wins, winnerText and init them inside updateStats method. then inside onViewCreated() method, set values like

m_ComputerWins.setText(Integer.toString(computer_wins));
 m_PlayerWins.setText(Integer.toString(player_wins));
  m_WinnerTextView.setText(winnerText);
Kishan Maurya
  • 3,356
  • 8
  • 21
  • I followed your advice and obtained the desired result. Nonetheless the other answers show also important hints. – Cristi Sep 20 '19 at 09:38
0

You never know when the fragments is created (which calls onCreate()), because fragment's life cycle differs from your activity's. In your code, you are trying to update fragment from activity. But in the way you have written your code, it is almost guaranteed that your fragment is not created yet.

Search for how to pass parameters to fragment or update fragment to find out the correct way of updating fragment from activity. You can find several related question, e.g. this one.

Afshin
  • 8,839
  • 1
  • 18
  • 53
0

FragmentTransaction only runs on the next event loop. If you want to immediately run a fragment transaction, use fragmentTransaction.commitNowAllowingStateLoss();.

Please note that this won't actually work correctly after process death, which is why you should use the fragment.setArguments(Bundle method to pass initial argument values to a fragment.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428