Repopulating Values ​​in Android Fragment onResume

I have a fragment (A) with a TextView with the value "XXXX" set using the setText () method. I replace fragment (A) with fragment (B) and then replace B again with A.

When I do this, the value XXXX will disappear in the (A) TextView snippet. I tried calling the TextView.setText method on onStart, as well as the onResume methods - the same result. When I debug the code, I can literally see that the setText method and value XXXX are used. I printed it on LogCat and it is there too, but I do not see any values ​​on the screen.

I tried google search and I could not get an answer. I would appreciate any pointers.

code

public void onResume() { super.onResume(); String dData = readFileFromSDCard(); String dArray[] = dData.split(";"); txtName = (TextView) getActivity().findViewById(R.id.txtName); txtName.setText("first name") } 
+4
source share
1 answer

With fragments, you cannot get the layout with getActivity().findViewById(R.id.txtName); You must do it like this. This is caused when also when replacing fragment B with A.

This code should stand in your Fragments class, of course.

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.your_layout, container, false); String driverData = readFileFromSDCard(); String driverArray[] = driverData.split(";"); txtName = (TextView) view.findViewById(R.id.txtName); txtName.setText("Andrew"); } 
+5
source

Source: https://habr.com/ru/post/1382497/


All Articles