मुझे लगता है कि आपके लिए सबसे अच्छा समाधान मुख्य गतिविधि के अंदर चर है और उन्हें टुकड़ों से एक्सेस करना है। मेरा मतलब है, अगर आपको सभी टुकड़ों में समान करना है, तो आप गतिविधि के अंदर कोड लिख सकते हैं और आपको जिस विधि की आवश्यकता है उसे कॉल करें।
public class Fragment_1 extends Fragment{
DataCommunication mCallback;
@Override
public void onAttach(Context context) {
super.onAttach(context);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
mCallback = (DataCommunication) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement DataCommunication");
}
}
...
:
आप इस उद्देश्य के लिए एक इंटरफेस का उपयोग करने
public interface DataCommunication {
public String getMyVariableX();
public void setMyVariableX(String x);
public int getMyVariableY();
public void setMyVariableY(int y);
}
फिर, उसे अपने गतिविधि के अंदर लागू
public class MainActivity extends Activity implements DataCommunication {
private String x;
private int y;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
}
...
@Override
public String getMyVariableX(){
return x;
}
@Override
public void setMyVariableX(String x){
//do whatever or just set
this.x = x;
}
@Override
public int getMyVariableY(){
return y;
}
@Override
public void setMyVariableY(int y){
//do whatever or just set
this.y = y;
}
...
फिर, सभी अपने टुकड़े में गतिविधि देते हैं की जरूरत है
और आखिरकार, जब आपको एक टुकड़े के अंदर एक चर का उपयोग करने की आवश्यकता होती है, तो बस ' बना लिया
https://developer.android.com/training/basics/fragments/communicating.html
यदि आप किसी भी उपयोगकर्ता-इंटरैक्शन के बिना डेटा देखते हैं (क्लिक करने के लिए कोई बटन नहीं, सूचीदृश्य चयन करने के लिए चयन करें ...)) आपको दृश्य-केवल-खंड से डेटा को किसी अन्य खंड में क्यों पारित करने की आवश्यकता है? पहले खंड में – k3b
मैं कई लक्षण बनाता हूं; दूसरों में समान लक्षणों की आवश्यकता होती है, इसलिए मैं कॉम्पेक्सिटी को कम करने और समय – Soufiane