ClickableSpan не сохраняет активность при ротации
Я устанавливаю clickablespan
на подстроке строки и показывая фрагмент диалога в onClick
,
Когда я щелкаю текст в первый раз, фрагмент диалога отображается нормально. После закрытия диалогового окна я поворачиваю экран и снова нажимаю текст, он выдает IllegalStateException: Activity has been destroyed
,
Когда я отладил, я обнаружил, что активность нулевая.
Это мой код:
String rawAuthor = String.format(getResources().getString(R.string.post_by), name, postDateStr);
int i1 = rawAuthor.indexOf(" by ");
int i2 = rawAuthor.indexOf(" on ");
postAuthorDate.setText(rawAuthor, TextView.BufferType.SPANNABLE);
postAuthorDate.setMovementMethod(LinkMovementMethod.getInstance());
postAuthorDate.setHighlightColor(ContextCompat.getColor(getActivity(), R.color.nav_bg));
Spannable spannable = (Spannable)postAuthorDate.getText();
ClickableSpan clickableSpan = new ClickableSpan() {
@Override public void onClick(View widget) {
Log.d(TAG, "I was clicked");
//mOnTagCatClickedListener.OnTatCatClicked(name, authorId, "Author");
TagCatFragment tagCatFragment = TagCatFragment.newInstance(name, authorId, "Author");
try {
tagCatFragment.show(getChildFragmentManager(), "TagCAatFragment");
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
};
spannable.setSpan(clickableSpan, i1+4, i2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Трассировки стека:
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: java.lang.IllegalStateException: Activity has been destroyed
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1515)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:638)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.support.v4.app.DialogFragment.show(DialogFragment.java:139)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at com.ozuf.poster.Fragment.DetailsFragment$9.onClick(DetailsFragment.java:474)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.text.method.LinkMovementMethod.onTouchEvent(LinkMovementMethod.java:217)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.widget.TextView.onTouchEvent(TextView.java:8464)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.View.dispatchTouchEvent(View.java:8808)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2581)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2607)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1779)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.app.Activity.dispatchTouchEvent(Activity.java:2846)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:60)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2568)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.View.dispatchPointerEvent(View.java:9003)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4209)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4072)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3680)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3646)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3763)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3654)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3820)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3680)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3646)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3654)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3627)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5910)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5884)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5855)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6000)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.os.MessageQueue.nativePollOnce(Native Method)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.os.MessageQueue.next(MessageQueue.java:143)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.os.Looper.loop(Looper.java:122)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5910)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
07-06 16:56:31.164 2162-2162/com.ozuf.poster W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)
Хотя я обнаружил, что это ошибка, пожалуйста, есть ли какое-то временное исправление?
1 ответ
Вы правы, есть ошибка. ребенок FragmentManager
заканчивается нарушенным внутренним состоянием, когда он отсоединен от activity
, Но просто вы называете это, чтобы исправить это с помощью onDetach
в вашем Fragment
,
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}