में चर्चा के अनुसार DialogFragment
सिस्टम का उपयोग करने के लिए, वर्तमान में Activity.showDialog(DIALOG_ID);
का उपयोग करके मैं अपने संवाद माइग्रेट कर रहा हूं।ओरिएंटेशन चेंज पर डायलॉगफ्रैगमेंट कॉलबैक
यहाँ है एक सरल संवाद के कुछ उदाहरण कोड:
public class DialogTest extends DialogFragment {
public interface DialogTestListener {
public void onDialogPositiveClick(DialogFragment dialog);
}
// Use this instance of the interface to deliver action events
static DialogTestListener mListener;
public static DialogTest newInstance(Activity activity, int titleId, int messageId) {
udateListener(activity);
DialogTest frag = new DialogTest();
Bundle args = new Bundle();
args.putInt("titleId", titleId);
args.putInt("messageId", messageId);
frag.setArguments(args);
return frag;
}
public static void udateListener(Activity activity) {
try {
// Instantiate the NoticeDialogListener so we can send events with it
mListener = (DialogTestListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString() + " must implement DialogTestListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
int titleId = getArguments().getInt("titleId");
int messageId = getArguments().getInt("messageId");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// dialog title
builder.setTitle(titleId);
// dialog message
builder.setMessage(messageId);
// dialog negative button
builder.setNegativeButton("No", new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {}});
// dialog positive button
builder.setPositiveButton("Yes", new OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(DialogTest.this);
}});
// create the Dialog object and return it
return builder.create();
}}
वहाँ एक सवाल है कि मेरे विकास के दौरान पैदा हुई जब कॉलबैक का उपयोग करके किसी घटना वापस गतिविधि/टुकड़ा है कि संवाद खोला करने के लिए वितरित करने के लिए है और यहां कुछ गतिविधि कोड इसे कॉल कर रहे हैं:
public class SomeActivity extends FragmentActivity implements DialogTestListener {
private EditText mUserName;
@Override
public void onCreate(Bundle savedInstanceState) {
// setup ui
super.onCreate(savedInstanceState);
setContentView(R.layout.ui_user_edit);
// name input
mUserName = (EditText) findViewById(R.id.userEdit_editTextName);
}
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
Log.d(TAG, this.toString());
mUserName.setText(mUserName.getText() + "1");
}
private void showDialog() {
DialogTest test = DialogTest.newInstance(SomeActivity.this, R.string.someTitleText, R.string.someMessageText);
test.show(getSupportFragmentManager(), "testDialog");
}}
कोड आप जो संदर्भ देखते हैं, वह काफी है। समस्या यह है कि, जब आप एक अभिविन्यास परिवर्तन करते हैं, जब एक संवाद दिखाया जाता है, तो यह अपेक्षित काम करना बंद कर देता है -> गतिविधि जीवन चक्र के कारण, गतिविधि और संवाद दोनों पुनर्निर्माण होते हैं, और संवाद में अब उचित नहीं है नई पुनर्निर्मित गतिविधि के संदर्भ में। ऐसा करने से
@Override
protected void onResume() {
super.onResume();
DialogTest.udateListener(this);
}
, मैं अपेक्षित व्यवहार मिलता है, और संवाद नई पुनर्निर्माण गतिविधि के लिए वापस घटनाओं भेजता है एक ओरिएंटेशन परिवर्तन हुआ:
मैं onResume विधि मेरी activitys के लिए निम्न कोड गयी।
मेरा प्रश्न है: डायलॉगफ्रैगमेंट के बीच कॉलबैक को संभालने के लिए 'सर्वोत्तम अभ्यास' क्या है जो एक अभिविन्यास परिवर्तन के दौरान फ्रैगमेंट एक्टिविटी द्वारा खोला गया था?
सादर
लगता है जैसे आप एक कई टुकड़े टुकड़े गिर गए हैं। मुझे आपके जैसा ही समस्या थी और मैं इस उत्कृष्ट आलेख को पढ़ने के दौरान इसे ठीक करने में सक्षम था: http://code.hootsuite.com/orientation-changes-on-android/। –