In my application, I have multiple instances of the same fragment. I want to be able to setText of a Textview within each individual fragment. However, when I try to setText of a fragment's Textview, it will change that Textview in every fragment.
How would I be able to change a Textview in an individual instance of a fragment, without using unique Tags or IDs for each fragment?
Here is my fragment class:
public static class ActivityFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, Bundle savedInstanceState) {
final View fragment1 = inflater.inflate(R.layout.activity_fragment, container, false);
final TextView activityText = (TextView) fragment1.findViewById(R.id.activity_text);
//Calling setText changes the activityText Textview in every fragment onscreen
activityText.setText(text);
return fragment1;
}
}
Here is my MainActivity class:
public class MainActivity extends FragmentActivity {
Static String text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i != 5; i++) {
text = "success" + i;
ActivityFragment myFragment = new ActivityFragment();
getFragmentManager().beginTransaction()
.add(R.id.fragment_container, myFragment).commit();
}
}