問(wèn)題描述
在更改方向后,我遇到了正確對(duì)話(huà)框片段解散的問(wèn)題.我猜這是由于舊的上下文,因?yàn)樵谥匦聞?chuàng)建該活動(dòng)之后,創(chuàng)建了一個(gè)新的活動(dòng)和相應(yīng)的上下文.我知道我可以在 onSaveInstance 中設(shè)置一些變量來(lái)保存對(duì)話(huà)框狀態(tài)并在必要時(shí)重新創(chuàng)建對(duì)話(huà)框,或者只是將一個(gè)屬性放在清單方向"中.但是,也許有更好的方法可以做到不在清單中硬編碼它而不是手動(dòng)保存在 onSaveIntance 中?我也嘗試在主片段和對(duì)話(huà)框片段中使用 setRetainInstance 但它對(duì)我沒(méi)有幫助.
I have a problem with correct dialog fragment dismissing after that orientation was changed. I guess that's due to old context because after that activity was recreated there was created a new activity and corresponding context. I know that I can set some variable in onSaveInstance to save dialog status and recreate dialog if it`s necessary, or just put one attribute in manifest "orientation". But, maybe, there is something better way to do it to not hardcode it in manifest and not save in onSaveIntance manually? I also tried to use setRetainInstance in both main fragment and dialog fragment but it doesn't help me.
片段:
public class MainFragment extends Fragment implements ServiceExecutorListener, OnClickListener, DialogClickListener {
private static final String TAG = MainFragment.class.getName();
private TextView serviceStatus;
Intent intent;
Boolean bound = false;
ServiceConnection sConn;
RESTService service;
ProgressDialogFragment pd;
private Button btnSend, btnCheck;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
setRetainInstance(true);
intent = new Intent(getActivity(), RESTService.class);
getActivity().startService(intent);
sConn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d(TAG, "MainFragment onServiceConnected");
service = ((RESTService.MyBinder) binder).getService();
service.registerListener(MainFragment.this);
if (service.tasksAreDone())
serviceStatus.setText(service.getResult());
bound = true;
}
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "MainFragment onServiceDisconnected");
bound = false;
}
};
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
serviceStatus = (TextView) rootView.findViewById(R.id.tvServiceStatusValue);
btnSend = (Button) rootView.findViewById(R.id.btnSend);
btnCheck = (Button) rootView.findViewById(R.id.btnCheck);
btnSend.setOnClickListener(this);
btnCheck.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnSend:
pd = new ProgressDialogFragment();
pd.newInstance(null);
pd.setDialogClickListener(this);
pd.show(getActivity().getSupportFragmentManager(), "ProgressDialog");
service.run(RESTService.REQUEST, 7);
service.run(RESTService.REQUEST, 2);
service.run(RESTService.REQUEST, 4);
break;
case R.id.btnCheck:
if (service != null)
serviceStatus.setText(String.valueOf(service.tasksAreDone()) + service.getTasksCount());
break;
}
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart: Bind service");
getActivity().bindService(intent, sConn, 0);
}
@Override
public void onPause() {
super.onPause();
Log.d(TAG, "onPause: Unbind service");
if (!bound)
return;
getActivity().unbindService(sConn);
service.unregisterListener();
bound = false;
}
@Override
public void onComplete(int taskID, int action, String result) {
Log.d(TAG, "Task #" + taskID + ", action = " + action + " Completed");
pd.dismiss();
serviceStatus.setText(result);
}
@Override
public void onDialogClick(int action) {
switch (action) {
case Dialog.BUTTON_POSITIVE:
Log.d(TAG, "POSITIVE BUTTON");
break;
case Dialog.BUTTON_NEGATIVE:
Log.d(TAG, "NEGATIVE BUTTON");
service.removeTasks();
break;
case Dialog.BUTTON_NEUTRAL:
Log.d(TAG, "NEUTRAL BUTTON");
break;
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
}
}
對(duì)話(huà)框:
public class ProgressDialogFragment extends DialogFragment implements OnClickListener {
final String TAG = ProgressDialogFragment.class.getName();
private DialogClickListener listener;
public ProgressDialogFragment newInstance(Bundle args) {
ProgressDialogFragment pdf = new ProgressDialogFragment();
pdf.setArguments(args);
return pdf;
}
public void setDialogClickListener(DialogClickListener listener) {
this.listener = listener;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Log.d(TAG, "onCreate");
}
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
.setTitle("Title!")
.setPositiveButton(R.string.yes, this)
.setNegativeButton(R.string.no, this)
.setNeutralButton(R.string.maybe, this)
.setCancelable(false)
.setMessage(R.string.message_text)
.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
return adb.create();
}
public void onClick(DialogInterface dialog, int which) {
if (listener != null)
listener.onDialogClick(which);
}
public void onDismiss(DialogInterface dialog) {
Log.d(TAG, "Dialog: onDismiss, dialog = " + getDialog() + ", retainInstance = " + getRetainInstance());
// Fix to avoid simple dialog dismiss in orientation change
if ((getDialog() != null) && getRetainInstance())
getDialog().setDismissMessage(null);
}
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
Log.d(TAG, "Dialog: onCancel");
}
LOgCat:
04-29 06:17:17.860: E/AndroidRuntime(4202): FATAL EXCEPTION: main
04-29 06:17:17.860: E/AndroidRuntime(4202): java.lang.NullPointerException
04-29 06:17:17.860: E/AndroidRuntime(4202): at android.support.v4.app.DialogFragment.dismissInternal(DialogFragment.java:184)
04-29 06:17:17.860: E/AndroidRuntime(4202): at android.support.v4.app.DialogFragment.dismiss(DialogFragment.java:155)
04-29 06:17:17.860: E/AndroidRuntime(4202): at com.example.restservice.fragments.MainFragment.onComplete(MainFragment.java:108)
04-29 06:17:17.860: E/AndroidRuntime(4202): at com.example.restservice.service.RESTService$1.run(RESTService.java:79)
04-29 06:17:17.860: E/AndroidRuntime(4202): at android.os.Handler.handleCallback(Handler.java:605)
04-29 06:17:17.860: E/AndroidRuntime(4202): at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 06:17:17.860: E/AndroidRuntime(4202): at android.os.Looper.loop(Looper.java:137)
04-29 06:17:17.860: E/AndroidRuntime(4202): at android.app.ActivityThread.main(ActivityThread.java:4514)
04-29 06:17:17.860: E/AndroidRuntime(4202): at java.lang.reflect.Method.invokeNative(Native Method)
04-29 06:17:17.860: E/AndroidRuntime(4202): at java.lang.reflect.Method.invoke(Method.java:511)
04-29 06:17:17.860: E/AndroidRuntime(4202): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
04-29 06:17:17.860: E/AndroidRuntime(4202): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
04-29 06:17:17.860: E/AndroidRuntime(4202): at dalvik.system.NativeStart.main(Native Method)
如果我將 setRetainInstance(true) 放在調(diào)用 dialogFragment 的主片段中,然后將其放在 onCreate 方法的 DialogFragment 中,那么我會(huì)看到 getRetainInstance 返回 true 并且 getDialog 在方向更改過(guò)程中具有對(duì)象(否則為 NPE).在這種情況下,我也沒(méi)有 NPE,但是會(huì)出現(xiàn)以下奇怪的行為:創(chuàng)建并呈現(xiàn)對(duì)話(huà)框,重新創(chuàng)建對(duì)話(huà)框(方向更改)并關(guān)閉(為什么?),重新創(chuàng)建對(duì)話(huà)框(再次更改方向)并呈現(xiàn)(wtf?上次它被解除了)等等,即對(duì)話(huà)在一側(cè)被解除,但請(qǐng)記住它應(yīng)該在另一側(cè)呈現(xiàn).那是什么?
If I put setRetainInstance(true) in main fragment which calls dialogFragment and in DialogFragment in onCreate method only then I see that getRetainInstance return true and getDialog has object in the process of orientation change(otherwise NPE). In this case I also don`t have NPE BUT there will be following strange behaviour: dialog created and presented, dialog recreated(orientation change) and dismissed(why?), dialog recreated(again orientation changed) and presented(wtf? last time it was dismissed) and so on i.e dialog dismissed on one side but remember that on another side it should be presented. What is that?
推薦答案
從 onCreateDialog(Bundle savedInstanceState)
中移除 setRetainInstance(true);
并保留在 onCreate(捆綁 savedInstance) 如下:
Remove the setRetainInstance(true);
from onCreateDialog(Bundle savedInstanceState)
and keep it in onCreate(Bundle savedInstance) as follows :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
這篇關(guān)于Android:為什么 DialogFragment 在方向更改時(shí)返回空指針的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!