關(guān)于Android Fragment對(duì)回退棧的詳細(xì)理解
測(cè)試項(xiàng)目
FragmentActivity
public class FragmentActivity extends AppCompatActivity {
private FrameLayout frameLayout;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
frameLayout = findViewById(R.id.fl_content);
button = findViewById(R.id.btn_click);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fTwo = new FragmentOne();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.fl_content, fTwo, "One");
tx.addToBackStack(null);//添加回退棧
tx.commit();
System.out.println("fm.getBackStackEntryCount() = " + fm.getBackStackEntryCount());
}
});
}
}FragmentOne(FragmentTwo、FragmentThree同理)
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View mView = inflater.inflate(R.layout.fragment_one, container, false);
editText = mView.findViewById(R.id.et_input);
button = mView.findViewById(R.id.btn_next);
System.out.println("mView.findViewById = "+editText);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fTwo = new FragmentTwo();
FragmentManager fm = getActivity().getSupportFragmentManager();
FragmentTransaction tx = fm.beginTransaction();
tx.replace(R.id.fl_content, fTwo, "TWO");
tx.addToBackStack(null);
tx.commit();
System.out.println( "fm.getBackStackEntryCount() = "+ fm.getBackStackEntryCount());
}
});
return mView;
}
@Override
public void onStop() {
super.onStop();
Log.d("Tina======>", "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.d("Tina======>", "onDestroyView"+editText);
}
@Override
public void onDetach() {
super.onDetach();
Log.d("Tina======>", "onDetach");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d("Tina======>", "onDestroy");
}
前提條件是添加fragmet的時(shí)候都是用的replace(先remove 后add)
操作1:全部添加回退棧的操作
生命周期開啟的時(shí)候,打開到第三個(gè)fragment

開啟.png
回退的時(shí)候

結(jié)束.png
操作2:fragmentOne不加回退棧,其他加
生命周期開啟的時(shí)候

開啟.png
回退的時(shí)候

結(jié)束.png
結(jié)論
1.replace 會(huì)銷毀視圖,生命周期走到了onDestroyView
2.加入回退棧,返回的時(shí)候,fragment還可以重新顯示出來,否則返回的時(shí)候,就直接銷毀了
3.加入回退棧后,返回時(shí)會(huì)重新走onCreateView方法,重新綁定視圖,也就是說,實(shí)例對(duì)象會(huì)變
舉例:recyclerview的話,返回來后,之前setAdapter就沒有了,因?yàn)閷?duì)象換了
注意有一個(gè)還會(huì)是原來的值,是因?yàn)樗鼘?duì)數(shù)據(jù)有保存,他就是edictText(要有id值)
到此這篇關(guān)于關(guān)于Android Fragment對(duì)回退棧詳細(xì)理解的文章就介紹到這了,更多相關(guān)Android Fragment回退棧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android Fragment 基本了解(圖文介紹)
- Android 管理Activity中的fragments
- Android基礎(chǔ)之使用Fragment控制切換多個(gè)頁面
- android開發(fā)教程之實(shí)現(xiàn)滑動(dòng)關(guān)閉fragment示例
- FrameLayout和Fragment處理Android應(yīng)用UI布局實(shí)例
- Android的Fragment的生命周期各狀態(tài)和回調(diào)函數(shù)使用
- Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)切換效果
- Android App中ViewPager與Fragment結(jié)合的一些問題解決
- Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
- 詳解Android應(yīng)用中DialogFragment的基本用法
相關(guān)文章
Flutter 用自定義轉(zhuǎn)場(chǎng)動(dòng)畫實(shí)現(xiàn)頁面切換
本篇介紹了 fluro 導(dǎo)航到其他頁面的自定義轉(zhuǎn)場(chǎng)動(dòng)畫實(shí)現(xiàn),F(xiàn)lutter本身提供了不少預(yù)定義的轉(zhuǎn)場(chǎng)動(dòng)畫,可以通過 transitionBuilder 參數(shù)設(shè)計(jì)多種多樣的轉(zhuǎn)場(chǎng)動(dòng)畫,也可以通過自定義的 AnimatedWidget實(shí)現(xiàn)個(gè)性化的轉(zhuǎn)場(chǎng)動(dòng)畫效果。2021-06-06
Android簡(jiǎn)單自定義音樂波動(dòng)特效圖
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單自定義音樂波動(dòng)特效圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
AndroidStudio 使用過程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說明,如何處理出現(xiàn)這種問題,有需要的小伙伴可以參考下2016-09-09
Android自定義View實(shí)現(xiàn)多圖片選擇控件
Android使用Gallery實(shí)現(xiàn)照片拖動(dòng)的特效
詳解Android使用Handler造成內(nèi)存泄露的分析及解決方法
AndroidImageSlider實(shí)現(xiàn)炫酷輪播廣告效果

