1 year ago

#341211

test-img

Taha Sami

setText method does not work inside the onViewCreated method

DetailsFragment -> onCreateView

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (fragmentBinding == null)
        fragmentBinding = FragmentDetails.inflate(inflater, container, false);
    return fragmentBinding.getRoot();
}

DetailsFragment -> onViewCreated

@Override
public void onViewCreated(@NonNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    int x = new Random().nextInt(10000);
    Log.w("Result", "... " + x);
    fragmentBinding.titleEditText.setText(String.valueOf(x));

}

When open DetailsFragment for the first time, titleEditText will take the generated number from the x variable (Let's suppose it 528)

Inside DetailsFragment there is a button that will navigate me to the BFragment fragment.

When navigating to the BFragment fragment and doing some tasks, I will press the back button for back to DetailsFragment

What do I expect the result?

When back from BFragment to DetailsFragment the x variable will take a new random number (Let's suppose it 362) and put it inside titleEditText.

What is the current result?

The text inside titleEditText still have 528 value but it must take 362 value

Notes

  • It's working well if I put the code inside the onResume method.
  • I'm using the navigation component library with backstack.

Improved the question (Added more details)

I built a simple app with 2 fragments to show you my problem

Watch the result via the video on streamable.com, Mr Cheticamp, If the video doesn't work tell me and I'll upload it to Youtube.

Here is the Logcat when running the app for the first time

enter image description here

And this is the Logcat when back from SecondFragment to FirstFragment

enter image description here

Here is the code for FirstFragment

public class FirstFragment extends Fragment {

    private FragmentFirstBinding fragmentFirstBinding;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.w("FirstFragment", "onCreate");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.w("FirstFragment", "onCreateView");
        if (fragmentFirstBinding == null)
            fragmentFirstBinding = FragmentFirstBinding.inflate(inflater, container, false);
        return fragmentFirstBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.w("FirstFragment", "onViewCreated");

        fragmentFirstBinding.btnFirst.setOnClickListener(view1 -> Navigation.findNavController(view1).navigate(FirstFragmentDirections.actionFirstFragmentToSecondFragment()));

        int x = new Random().nextInt(10000);
        fragmentFirstBinding.edittext.setText(String.valueOf(x));
        Log.w("FirstFragment - X Value", "... " + x);
    }

}

And here is the code for SecondFragment

public class SecondFragment extends Fragment {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_second, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.btnSecond).setOnClickListener(view1 -> Navigation.findNavController(view1).popBackStack());
    }

}

android

android-fragments

android-architecture-navigation

0 Answers

Your Answer

Accepted video resources