英文:
When I rotate the screen, the fragment is destroyed and returned to Activity, which opened that Fragment. But why he's doing so?
问题
我有一个可以通过两个不同按钮打开两个不同Fragment的活动。默认情况下,当该活动创建时,它会打开一个我们称之为“主Fragment”的Fragment。
我们通过第一个按钮跳转到的第一个Fragment在旋转时没有任何问题,但是第二个Fragment在旋转后消失了,屏幕显示了主Fragment的内容。当我尝试将屏幕旋转回来时,我再次看到了主Fragment的内容。但是为什么会这样呢?我没有编写任何代码,应该在不点击按钮的情况下返回主Fragment。
你有什么假设?
英文:
I have an Activity which can open 2 different Fragments by 2 different Buttons. By the default that Activity when it creates, it is opening a Fragment, we call it "The Main 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. When I tried to rotate the screen back, I see the Main Fragment's content again. 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?
答案1
得分: 3
为什么会发生这种情况?
默认行为,屏幕方向变化时会重新创建Activity,因此您的Fragment也会重新创建。
解释
您需要了解Activity生命周期,才能理解为什么会发生这种情况。
首先,“旋转屏幕”不是我们今天讨论的实际情况。因为任何配置更改都会导致Android重新启动您的Activity。配置更改可能是设备旋转(因为现在我们有不同的屏幕布局要绘制),也可能是语言切换(因为我们需要重新编写所有这些字符串,可能需要更多的空间,或者可能是可怕的RTL切换!),甚至是键盘的可用性。
通过重新加载您的应用程序,系统实际上是调用了onDestroy(),然后立即调用onCreate()。这样,您的Activity尽可能新鲜,具有所有正确的创建数据(即使用户一直与您在一起)。
现在您有以下选项 -
-
要么从
AndroidManifest.xml
中为您的应用程序修复方向<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden">
但显然这对用户来说不是一个很好的体验。
-
使用
onSaveInstanceState()
保存activityState此方法将在onDestroy()之前调用。当您的Activity被创建时,还有一个匹配的onRestoreInstanceState()步骤,这也将自动调用。所有这些自动步骤意味着您可以让系统担心保存和加载您的数据,因为您提前计划并映射出了重要的内容。(或者,您可以跳过onRestoreInstanceState(),并从onCreate()附带的Bundle中加载已保存的状态)。
如果在Activity中集成Fragment,因为Activity被销毁(),所以您的Fragment也会被销毁(),然后重新创建()。
一旦您理解了这些概念,事情就会开始变得清晰,但只有在您完成学习曲线时才会发生这种情况。
编码愉快!
英文:
Why this is happening ?
> Default Behavior, Actiivty is getting recreated on orientation change so your fragment are.
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. 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!), or even keyboard availability.
By reloading your app, what the system is actually doing is calling onDestroy() and then immediately calling 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).
Now you have following option -
-
Either Fix Orientation for your app from
AndroidManifest.xml
<activity android:screenOrientation="landscape" android:configChanges="orientation|keyboardHidden">
But oviously that is not a very good experience for user.
- Save activityState with
onSaveInstanceState()
This method will be called before onDestroy(). And, when your Activity is created, there’s a matching step onRestoreInstanceState(), which will also be called automatically. 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().
In you integrate Fragment in activity, because activity is getting destroy() so your fragment will also destroy() and will be recreated.
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 !
答案2
得分: 1
这是因为每次屏幕旋转时都会调用onCreate
。可能你正在从你的onCreate
方法中显示The Main Fragment
。如果将片段显示逻辑放在onResume
中,你将面临相同的问题,因为在onCreate
之后,会调用onResume
。
解决方案:将片段存储在共享首选项中,这样每次调用onCreate
时,你都知道要显示什么。
英文:
That is because onCreate
is being called every time the screen is rotated. Probably you are displaying The Main Fragment
from your onCreate
method. You will face the same issue if you put your fragment display logic in onResume
because just after onCreate, onResume is called.
Solution: store the fragment on top in shared preferences that way you know what to display every time onCreate is being called.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论