简体   繁体   English

当我旋转屏幕时,片段被销毁并返回到 Activity,它打开了那个片段。 但他为什么要这样做?

[英]When I rotate the screen, the fragment is destroyed and returned to Activity, which opened that Fragment. But why he's doing so?

I have an Activity which can open 2 different Fragments by 2 different Buttons.我有一个 Activity 可以通过 2 个不同的按钮打开 2 个不同的片段。 By the default that Activity when it creates, it is opening a Fragment, we call it "The Main Fragment".默认情况下,Activity在创建时,会打开一个Fragment,我们称之为“主Fragment”。 The first Fragment to which we are going over by the first Button we meet zero problems with the Rotation, but the second one after the rotation disappears and the screen shows the Main Fragment's content.我们通过第一个 Button 遍历的第一个 Fragment 我们遇到了 Rotation 零问题,但旋转后的第二个 Fragment 消失了,屏幕显示了 Main Fragment 的内容。 When I tried to rotate the screen back, I see the Main Fragment's content again.当我尝试将屏幕旋转回来时,我再次看到了 Main Fragment 的内容。 But why it is so, if I didn't write any code, which must return me to the Main Fragment without clicking a button.但是为什么会这样,如果我没有编写任何代码,它必须在不单击按钮的情况下将我返回到主片段。

What assumptions do you have?你有什么假设?

Why this is happening?为什么会这样?

Default Behavior, Actiivty is getting recreated on orientation change so your fragment are.默认行为,Activity 会在方向更改时重新创建,因此您的片段也是如此。

Explanation解释

You need to understand Activity Life Cycle to understand why this is happening.您需要了解活动生命周期以了解为什么会发生这种情况。

在此处输入图像描述

First, “rotating the screen” is not the actual scenario we are talking about today.首先,“旋转屏幕”并不是我们今天要讲的实际场景。 Because any configuration change will cause Android to restart your Activity.因为任何配置更改都会导致 Android 重新启动您的 Activity。 A configuration change might be the device rotating (because now we have a different screen layout to draw upon), or it could be a language switch (because we need to re-write all those strings, which may need more room now OR it could be the scary RTL switch,).配置更改可能是设备旋转(因为现在我们有不同的屏幕布局可供绘制),也可能是语言切换(因为我们需要重新编写所有这些字符串,现在可能需要更多空间,或者它可以成为可怕的 RTL 开关,)。 or even keyboard availability.甚至键盘可用性。

By reloading your app, what the system is actually doing is calling onDestroy() and then immediately calling onCreate().通过重新加载您的应用程序,系统实际上正在调用 onDestroy(),然后立即调用 onCreate()。 This way, your Activity is as fresh as possible, with all of the right creation data (even though the user has been with you the entire time).这样,您的 Activity 将尽可能新鲜,并包含所有正确的创建数据(即使用户一直与您在一起)。

Now you have following option -现在您有以下选项 -

  1. Either Fix Orientation for your app from AndroidManifest.xmlAndroidManifest.xml为您的应用程序修复方向

But oviously that is not a very good experience for user.但显然这对用户来说并不是一个很好的体验。

  1. Save activityState with onSaveInstanceState()使用onSaveInstanceState()保存活动状态

This method will be called before onDestroy().此方法将在 onDestroy() 之前调用。 And, when your Activity is created, there's a matching step onRestoreInstanceState(), which will also be called automatically.而且,当您的 Activity 创建时,有一个匹配的步骤 onRestoreInstanceState(),它也会被自动调用。 All of these automatic steps mean that you can let the system worry about saving and loading your data, because you planned ahead and mapped out what was important.所有这些自动步骤意味着您可以让系统担心保存和加载数据,因为您提前计划并制定了重要的计划。 (Or, you can skip onRestoreInstanceState() and load your saved state from the Bundle that comes with onCreate(). (或者,您可以跳过 onRestoreInstanceState() 并从 onCreate() 附带的 Bundle 中加载您保存的 state。

In you integrate Fragment in activity, because activity is getting destroy() so your fragment will also destroy() and will be recreated.在您将 Fragment 集成到活动中时,因为活动正在销毁(),所以您的片段也将销毁()并将被重新创建。

在此处输入图像描述

Please take a good read on Handling Configuration Change and this .请仔细阅读处理配置更改这个

Once you understood the concepts things will start falling into your but it will only happen if you will complete your learning curve.一旦你理解了这些概念,事情就会开始落入你的脑海,但只有在你完成学习曲线的情况下才会发生。

Happy Coding !快乐编码!

That is because onCreate is being called every time the screen is rotated.那是因为每次屏幕旋转时都会调用onCreate Probably you are displaying The Main Fragment from your onCreate method.可能您正在通过onCreate方法显示The Main Fragment You will face the same issue if you put your fragment display logic in onResume because just after onCreate, onResume is called.如果您将片段显示逻辑放在onResume中,您将面临同样的问题,因为在 onCreate 之后会调用 onResume。

Solution: store the fragment on top in shared preferences that way you know what to display every time onCreate is being called.解决方案:将片段存储在共享首选项中,这样您就知道每次调用 onCreate 时要显示什么。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM