日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2024/4/15 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Activity内嵌Fragment,当Activity recreate时Fragment被添加多次,造成相互遮盖 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題描述:由于某些原因(如旋轉屏幕,或內存不足時)造成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被添加多次,造成相互遮盖的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 成人欧美视频在线观看 | 99久久国产热无码精品免费 | 香蕉视频免费看 | 国产精品毛片一区二区在线看舒淇 | 视频一区欧美 | 日韩毛片一区二区三区 | 99黄色| 免费观看一级黄色片 | 亚洲色婷婷久久精品av蜜桃 | 精品久久久久久久久久久久久 | 欧美亚洲一区二区三区四区 | 永久看看免费大片 | 色婷婷一区二区 | 国产色影院| 在线观看国产黄 | 久久av喷吹av高潮av萌白 | 成人欧美一区二区三区黑人动态图 | 图片区亚洲色图 | 日本国产三级xxxxxx | 国产白袜脚足j棉袜在线观看 | 亚洲 高清 成人 动漫 | 亚洲色欧美 | 国产91精品一区 | 91青青草 | 日韩免费看 | 夜夜操导航 | 日韩人妻精品一区二区三区 | 蜜桃久久久aaaa成人网一区 | 婷婷视频一区 | 日韩欧美亚洲一区二区三区 | 不用播放器av | 另类一区| 欧美日韩激情在线一区二区三区 | 色汉综合 | 变态视屏| 久久94| 日韩精品乱码 | 深夜久久 | 91调教打屁股xxxx网站 | 国产成年妇视频 | 欧美日韩一本 | 一区二区三区久久久久 | 亚洲清色 | 亚洲高清成人 | 国产一道本 | 777毛片 | 欧美久久一区二区 | 中国老妇性视频 | 超黄av| 久久精品高清 | 一区二区 中文字幕 | 一区二区在线视频观看 | 日韩视频成人 | 久久资源在线 | 亚洲毛片在线播放 | 动漫女被黄漫免费视频 | 91偷拍视频 | 国产精品www色诱视频 | 华人永久免费 | a在线看| 黄网免费在线观看 | 全部免费毛片在线播放 | 成人激情免费视频 | 色综合社区 | 三级在线网站 | 国产又粗又猛又大爽 | 日本打屁股网站 | 亚洲69| 色视频在线观看 | 午夜精品国产精品大乳美女 | 性生活视频在线播放 | 国产资源站| 欧美在线免费观看视频 | 国产69精品久久久久999小说 | 69色视频| 人人射人人 | 手机av在线看 | 在线看成人av | 日本女v片| 日屁网站 | 亚欧激情| 五月综合久久 | 影音先锋久久久久av综合网成人 | 日韩欧美视频免费观看 | 亚洲一二三av | 播金莲一级淫片aaaaaaa | 三级视频久久 | 久久成年网 | 激情小说五月天 | 欧美 日韩 国产 一区 | 国产chinesehd精品| 国产精品入口 | av综合在线观看 | 中文字幕一区三区 | 免费一区二区在线观看 | av在线电影网 | 超碰777 | 亚洲国产日韩欧美在线观看 | 可以免费观看av的网站 |