Android ViewPager 方向改变

2023-10-03移动开发问题
0

本文介绍了Android ViewPager 方向改变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我已阅读 @antonyt 的 对此 StackOverflow 问题,FragmentPagerAdapter 将在方向更改时尝试重用 FragmentManager.findFragmentByTag() 找到的现有片段.

I have read from @antonyt's answer to this StackOverflow question that FragmentPagerAdapter will try to reuse an existing fragment found by FragmentManager.findFragmentByTag() upon orientation change.

我已经试过了.FragmentPagerAdaptergetItem(int) 在第一次需要 Fragment 时被调用.在方向改变时,getItem 不会被调用,这与 @antonyt's 的回答一致.但是为什么又调用了FragmentonCreate()呢?我以为它没有被破坏?

I have tried this. getItem(int) of the FragmentPagerAdapter is called when the Fragment is first needed. Upon orientation change, getItem is not called, which is in accordance with @antonyt's answer. But why is it that onCreate() of the Fragment is called again? I thought it was not destroyed?

推荐答案

所有没有设置setRetainInstance(true)的片段在方向改变时被销毁.方向改变后,所有片段都由片段管理器重新创建和重新附加(因此片段贯穿整个生命周期).

All fragments which not set setRetainInstance(true) are destroyed on orientation change. After orientation changed all fragments are recreated and reattach by fragment manager (so fragment goes throught entire lifecycle).

ViewPager 附加的所有片段都被添加到带有特殊标签的片段管理器中.ViewPager 尝试重用在方向改变后重新创建的片段(通过在片段管理器中搜索标签).如果这样的片段不存在,则调用 FragmentPagerAdapter.getItem(int) 来创建新片段.

All fragment attached by ViewPager are added to fragment manager with special tag. ViewPager try to reuse fragment recreated after orientation changed (by searching for tag in fragment manager). If such fragment don't exist then FragmentPagerAdapter.getItem(int) is called to create new fragment.

这篇关于Android ViewPager 方向改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7

Sqlite 数据库未从资产文件夹 Android 复制
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)...
2024-04-15 移动开发问题
8

SQLite 数据库副本在由设备而不是模拟器生成时出现损坏
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)...
2024-04-15 移动开发问题
4

安卓文件拷贝
Android file copy(安卓文件拷贝)...
2024-04-15 移动开发问题
6

Android如何在android中检测Edittext的Copy事件
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)...
2024-04-15 移动开发问题
5