“java.lang.IllegalStateException:Activity 已被破坏”当按下 onBackPressed()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21332035/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"java.lang.IllegalStateException: Activity has been destroyed" when press onBackPressed()
提问by Adriana Carelli
I have an activity with an animated fragment. When I press on backpressed to close the activity I get this error. What does it mean?
我有一个带有动画片段的活动。当我按下 backpressed 关闭活动时,我收到此错误。这是什么意思?
01-24 12:41:24.407: E/AndroidRuntime(23621): FATAL EXCEPTION: main
01-24 12:41:24.407: E/AndroidRuntime(23621): java.lang.IllegalStateException: Activity has been destroyed
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1351)
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.app.BackStackRecord.commit(BackStackRecord.java:574)
01-24 12:41:24.407: E/AndroidRuntime(23621): at com.eni.enim4s.TranslucentActivity.run(TranslucentActivity.java:70)
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.os.Handler.handleCallback(Handler.java:725)
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.os.Handler.dispatchMessage(Handler.java:92)
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.os.Looper.loop(Looper.java:176)
01-24 12:41:24.407: E/AndroidRuntime(23621): at android.app.ActivityThread.main(ActivityThread.java:5279)
01-24 12:41:24.407: E/AndroidRuntime(23621): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 12:41:24.407: E/AndroidRuntime(23621): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 12:41:24.407: E/AndroidRuntime(23621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
01-24 12:41:24.407: E/AndroidRuntime(23621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
01-24 12:41:24.407: E/AndroidRuntime(23621): at dalvik.system.NativeStart.main(Native Method)
My code:
我的代码:
public class TranslucentActivity extends Activity {
private Fragment fragment;
private FrameLayout frame;
private android.app.FragmentTransaction ft;
private android.app.FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.translucent);
frame =(FrameLayout)findViewById(R.id.container);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
frame.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
fragment = new ViewDetail();
fragmentManager = getFragmentManager();
ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
ft.addToBackStack(null);
ft.replace(R.id.container, fragment, "Dettaglio");
ft.commit();//the error is here
}
}, 500);
}
@Override
public void onBackPressed() {
super.onBackPressed();
this.finish();
}
}
}
采纳答案by vipul mittal
you can just remove this onBackPressed
as android implicitly finishes your activity on back key pressed.
您可以将其删除,onBackPressed
因为 android 会在按下后退键时隐式完成您的活动。
And don't call addToBackStack
:
不要打电话addToBackStack
:
ft.addToBackStack(null);
remove the above line this adds your fragment transaction in backstack and on back key pressed first transaction is reverted and then activity is finished.
删除上面的行,这会在后台堆栈中添加您的片段事务,并在按下返回键时恢复第一个事务,然后完成活动。
If you remove this back press will finish the activity.
如果您删除此后按将完成活动。
change to:
改成:
if(hasFocus){
frame.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
fragment = new ViewDetail();
fragmentManager = getFragmentManager();
ft = fragmentManager.beginTransaction();
ft.setCustomAnimations(R.animator.trans_left_in,R.animator.trans_left_out);
ft.addToBackStack(null);
ft.replace(R.id.container, fragment, "Dettaglio");
ft.commit();//the error is here
}
}, 500);
}
回答by Digvesh Patel
@Override
public void onBackPressed() {
super.onBackPressed()
}
Remove this line this.finish() ;
删除此行 this.finish() ;
actually super.onBackPressed()
finish your activity
实际 super.onBackPressed()
完成你的活动
回答by desseim
The error is not happening on the line
错误没有发生在线上
this.finish();//the error is here
it's most likely happening on
它很可能发生在
ft.commit();
What happens is that, when you see this error, this code runs afterthe activity has been destroyed, so there is no FragmentManager
or any Activity
state to work with, thus the transaction cannot be committed.
发生的情况是,当您看到此错误时,此代码在活动被销毁后运行,因此没有FragmentManager
或任何Activity
状态可使用,因此无法提交事务。
This happens because you run this code after a delay, and if you press back the window focus changes and the task gets scheduled, but the activity is destroyed before the task actually starts.
发生这种情况是因为您在延迟后运行此代码,如果您按下回键,窗口焦点会发生变化并且任务被安排,但活动在任务实际开始之前被销毁。
What you could do is add a if (!isFinishing())
check before you run your UI changes inside the task, but I would advise that you rethink your UI flow a little bit. Ifyou want the transaction to run after a delay but before the app finishes, then move the finish()
instruction to the end of the task's run()
(whether this is UI-wise a good user experience or not).
您可以做的是if (!isFinishing())
在任务中运行 UI 更改之前添加检查,但我建议您重新考虑一下您的 UI 流程。如果您希望事务在延迟之后但在应用程序完成之前运行,则将finish()
指令移至任务的末尾run()
(无论这是否是 UI 方面良好的用户体验)。
Note also that you shouldn't have to do both
另请注意,您不必同时执行这两项操作
super.onBackPressed();
this.finish();//the error is here
in onBackPressed()
: either you do some custom action (e.g. finishing the app) to replacethe default behavior, oryou leave it to the original (super
) behavior.
in onBackPressed()
:要么您执行一些自定义操作(例如完成应用程序)来替换默认行为,要么将其保留为原始 ( super
) 行为。