Just to make sense how to get data from multiple instances of same fragment.
To simulate your example. You,
- Have a SampleFragment
- Have three instances of SampleFragment
- Get value of EditText from an instance
Sample fragment
public class SampleFragment extends Fragment {
private EditText mSampleEditText;
public String getSampleValue(){
String value;
if(mSampleEditText != null){
value = mSampleEditText.getText().toString();
}
return value;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mSampleEditText= (EditText) getActivity().findViewById(R.id.editText_sample);
}
}
Sample activity
public class SampleActivity extends AppCompatActivity {
String mInstance1Tag = "instance1";
String mInstance2Tag = "instance2";
String mInstance3Tag = "instance3";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
// First instance
transaction.replace(R.id.fragment_container1, new SampleFragment(), mInstance1Tag);
// Second instance
transaction.replace(R.id.fragment_container2, new SampleFragment(), mInstance2Tag);
// Third instance
transaction.replace(R.id.fragment_container3, new SampleFragment(), mInstance3Tag);
transaction.commit();
}
private String getValueFromInstance(String instanceTag) {
SampleFragment fragment = (SampleFragment) getSupportFragmentManager()
.findFragmentByTag(instanceTag);
return fragment.getSampleValue();
}
}
EDIT
After your comment, the question is more clear and also you are right on the questions about tags like 'what would", "where you have".
I'm going to be having multiple instances of the same fragments,
Before start my post, i had supposed you'll have specified fragment instance count. So i set instance count to 3 in my example and then paired instances and tags. Because if you add fragment instances via unique tags, then you can get them.
And dont get stuck on their tags, to make simple i defined them as "instance1", "instance2"...