问题描述:由于某些原因(如旋转屏幕,或内存不足时)造成Activity被destroy ,当再次回到该Activity时,系统会recreate 该Activity , if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. 如果在该Activity的onCreate中存在add Fragment的代码(未区分是create 还是recreate activity),则会再次add一次该Fragment,造成该Activity中会内嵌多个相同的Fragment. 所以最终原因还是由于Activity的生命周期造成的。

Recreating an Activity

There are a few scenarios in which your activity is destroyed due to normal app behavior, such as when the user presses the Back button or your activity signals its own destruction by calling finish(). The system may also destroy your activity if it's currently stopped and hasn't been used in a long time or the system needs to free up resources and must shut down cached processes to recover memory.

When your activity is destroyed because the user presses Back or the activity finishes itself, all traces of the Activity instance is gone forever. However, if the system destroys the activity due to system constraints (rather than normal app behavior), then although the actual Activity instance is gone, the system remembers that it existed such that if the user navigates back to it, the system creates a new instance of the activity using a set of saved data that describes the state of the activity when it was destroyed.The saved data that the system uses to restore the previous state is called the "instance state" and is a collection of key-value pairs stored in a Bundle object.

Caution: Your activity will be destroyed and recreated each time the device configuration changes, like when the user rotates the screen.When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).

By default, the system uses the Bundle instance state to save information about each View object in your activity layout (such as the text value entered into an EditText object). So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more transient state information that you'd like to restore, such as member variables that track the user's progress in the activity.

Note: In order for the Android system to restore the state of the views in your activity, each view must have a unique ID, supplied by the android:id attribute.

解决方案1:在add Fragment时,区分一下create 状态和recreate 状态

public class MainActivity extends FragmentActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.news_articles);// Check that the activity is using the layout version with// the fragment_container FrameLayoutif (findViewById(R.id.fragment_container) != null) {// However, if we're being restored from a previous state,// then we don't need to do anything and should return or else// we could end up with overlapping fragments.if (savedInstanceState != null) {return;}// Create a new Fragment to be placed in the activity layoutHeadlinesFragment firstFragment = new HeadlinesFragment();// In case this activity was started with special instructions from an// Intent, pass the Intent's extras to the fragment as argumentsfirstFragment.setArguments(getIntent().getExtras());// Add the fragment to the 'fragment_container' FrameLayoutgetSupportFragmentManager().beginTransaction().add(R.id.fragment_container, firstFragment).commit();}}
}

请详细看下如下注释:

            // However, if we're being restored from a previous state,// then we don't need to do anything and should return or else// we could end up with overlapping fragments.
解决方案2:在add Fragment时给该Fragment添加一个Tag,在onCreate中addFragment时给该Fragment增加一个Tag.在onCreate方法中添加Fragment,先findFragmentByTag,如果存在则不用添加该Fragment,如果不存在该Fragment,则添加一个。

Activity内嵌Fragment,当Activity recreate时Fragment被添加多次,造成相互遮盖相关推荐

  1. Android Activity内嵌Fragment,当Activity recreate时Fragment被添加多次,造成界面重叠

    由于某些原因,例如旋转屏幕,内存不足时,造成Activity被destroy() ,或者切换语言环境时,我们recreate()该Activity.这时,如果在该Activity的onCreate中未 ...

  2. Activity内嵌Fragment,当Activity recreate时Fragment出现重叠

    recreate() Activity recreate()之后会出现fragment重叠的问题,需要给fragment增加tag @Override protected void onCreate( ...

  3. PPT内嵌视频(指发布时只需要ppt一个文件即可)

    做实验时用手机拍了视频,想把视频嵌入到PPT中.只是单纯的嵌入很容易,但是我想将PPT推送给其他人时,不需要再传视频文件.搜了一下做法,可以通过flash视频格式实现.电脑为thinkpad笔记本,w ...

  4. 当内嵌图层有图层样式时,如何在其上边创建图层剪切蒙版

    http://www.xueui.cn/tutorials/icon-tutorials/teach-you-to-draw-a-switch.html

  5. Razor Page Library:开发独立通用RPL(内嵌wwwroot资源文件夹)

    Demo路径:https://github.com/yanshengjie/RPL.Demo 1. Introduction Razor Page Library 是ASP.NET Core 2.1引 ...

  6. mongodb java 内嵌文档_MongoDB 内嵌文档

    MongoDB是文档型的数据库系统,doc是MongoDB的数据单位,每个doc相当于关系型数据库的数据行(row),doc和row的区别在于field的原子性:row中的column是不和分割的原子 ...

  7. vs2015c语言内嵌汇编,C#中内嵌资源的读取

    起因 作为一个从Cpper转到C#并且直接从事WPF开发的萌新来说,正式编码过程中碰到了不少问题,一路上磕磕碰碰的.因为软件设计需求上的要求,需要将一些配置文件(XML.INI等)内嵌到程序中,等需要 ...

  8. java derby 网络模式_Derby 使用的2种方式:内嵌和独立

    Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目.由于是用Java实现的,所以可以在任何平台上运行:另外一个特点是体积小,免安装,只需要几个小jar包就可以运行了. ...

  9. Derby 使用的2种方式:内嵌和独立

    Derby数据库是一个纯用Java实现的内存数据库,属于Apache的一个开源项目.由于是用Java实现的,所以可以在任何平台上运行:另外一个特点是体积小,免安装,只需要几个小jar包就可以运行了. ...

最新文章

  1. 找不到工作,一程序员去帮屠夫卖猪肉 | 每日趣闻
  2. php 将二维数组合并,PHP二维数组合并排重的两种方式
  3. [翻译] OrigamiEngine
  4. wince下的蓝牙串口通信
  5. 前端之JQuery(二)
  6. boost::gil::compute_hessian_responses用法的测试程序
  7. Linux下的sleep()和sched_yield()
  8. UIProgressView 圆角
  9. VScode新建自定义模板快捷方式
  10. 【v3.6.2】iNeuOS工业互联网操作系统,发布实时存储方式:实时存储、变化存储、定时存储,设备振动状态和电能状态监测驱动...
  11. python中注释的快捷键_【Python】注释
  12. filter 灰度处理:公祭日,一行代码让页面变成黑白色调
  13. JavaScript 之 面向对象 [ 原型 ]
  14. Illustrator中文版教程,如何在 Illustrator 中为创建的图形添加颜色?
  15. python数据包pandas_python | 数据分析(二)- Pandas数据包
  16. 5 步教你将 MRS 数据导入 DWS
  17. 计算机新建里没有word,为什么电脑右键新建里头没有WORD文档,如何加上
  18. android 触摸屏校准,android实现触摸屏校准
  19. Unity Shader 内置函数
  20. 达梦dm8可视化工具_DM8(达梦8)数据库安装和使用

热门文章

  1. .net core Entity Framework Core Code First 框架 分层开发
  2. PMAC运动程序例程(一)
  3. 关于dismissViewControllerAnimated值得注意的一点(deinit)
  4. jwPlayer实现支持IE8及以下版本避免出错的方法
  5. Controls 属性与继承 TShape 类的小练习
  6. Linux各个文件夹的作用~~~非常实用!!
  7. 20155216 Exp5 MSF基础应用
  8. 【BZOJ 3165】 [Heoi2013]Segment 李超线段树
  9. python-mysql驱动64位
  10. 如何让一个对话框全屏对话框