简体   繁体   English

系统终止应用程序后,碎片会如何处理?

[英]What happens to fragments after the system kills an app?

"If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process.". “如果活动被暂停或停止,则系统可以通过要求活动结束或直接终止其进程来将其从内存中删除。”。 When the user goes back to the activity, it restores it's state with a bundle. 当用户返回到活动时,它将通过捆绑软件恢复其状态。

My question is: 我的问题是:

is it important to do this in oncreate: 在oncreate中执行此操作是否重要:

if(savedinstance != null)
{
    fragement = fm.findFragmentByTag("tag");
}
else
{
    fragment = new Fragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.webViewFrame,fragment,"tag");
    ft.commit()
}

instead of just this: 而不只是这个:

    fragment = new Fragment();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.add(R.id.webViewFrame,fragment,"tag");
    ft.commit()

If you are correctly saving the state of your activity and fragment in onSaveInstanceState() , and you want your activity to be recreated in the state it had before it was killed, you should use the first block of code you posted. 如果要在onSaveInstanceState()中正确保存活动和片段的状态,并且希望以活动被杀死之前的状态重新创建活动,则应使用发布的第一段代码。

The FragmentManager saves its state, and when recreated, restores the fragments it was holding when the activity was killed. FragmentManager保存其状态,并在重新创建时恢复其在活动被杀死时所持有的片段。 It steps them through the build-up lifecycle events using the fragment's saved state: create, createView, start, resume. 它使用片段的保存状态(创建,createView,启动,恢复)逐步引导他们完成构建生命周期事件。

I'm pretty sure if you try running the second block of code, you will find after restart that you have two instances of your fragment in the FragmentManager --the one added when the activity was first create, and the one added after restart. 我很确定,如果您尝试运行第二个代码块,则在重新启动后会发现FragmentManager有两个片段实例–一个是在首次创建活动时添加的,另一个是在重新启动后添加的。

For this all to work correctly, you must carefully save the state of both your activity and fragment(s) in the onSaveInstanceState() method of each, and then in onCreate() test savedInstanceState and when not null, use the bundle to restore the state of your activity/fragment. 为了使这一切正常进行,您必须在每个活动的onSaveInstanceState()方法中仔细保存活动和片段的状态,然后在onCreate()测试savedInstanceState ,如果不为null,请使用捆绑包还原您的活动/片段状态。

This is the guide for saving/restoring activity state . 这是保存/恢复活动状态的指南。 More info here . 更多信息在这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当某人关闭或杀死某个应用时代码会发生什么变化? - What happens to the code when someone closes or kills an app? 当应用程序在后台时活动和片段会发生什么 - What happens to activities and fragments when app is in background 应用更新后,活动生命周期会怎样? - What happens to the Activity lifecycle after an app update? 系统在onStop()状态处于闲置状态后杀死了我的android应用。 如何避免或发现这种情况? - System kills my android app after some idle in onStop() state. How to avoid or detect this situation? 系统杀死后的Android活动娱乐 - Android activity recreation after system kills it 当Android使用嵌套片段和getSupportFragmentManager()在方向更改时重新创建我的应用时,会发生什么情况? - What exactly happens when Android recreates my app on an orientation change using nested fragments and getSupportFragmentManager()? 在ViewPager下销毁的片段会发生什么情况? - What happens to fragments destroyed under ViewPager? 从应用商店更新应用后,数据库会发生什么? - What happens with the database after app update from app store? 用户杀死应用程序(android)后会清除什么? - What is cleared when a user kills an app (android)? 用户杀死应用后,Android重新创建活动 - Android recreating activity after user kills the app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM