這個程序之后我需要reload fragment 從Fragment A到Fragment B,再從Fragment B到Fragment C,然后做注冊程序,注冊正確后,popBackStack Fragment A和我的Fragment A會被重新加載。我的主要問題是片段 A 沒有重新加載 請幫助我
uj5u.com熱心網友回復:
您試圖實作的實際上是訪問 backStack 時的跳過操作。
對此有兩種方法
- 像這樣給你的每個片段一個標簽
transaction.add(R.id.main_activity_container, FirstFragment, "FirstFragment");
transaction.replace(R.id.main_activity_container, Second, "SECOND");
transaction.replace(R.id.main_activity_container, Third, "THIRD");
然后覆寫 onBackPressed 以檢查用戶是否在按下后退按鈕時在第三個片段中應該轉到第一個片段。
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getFragments().get(lastStack).getTag().equalsIgnoreCase("THIRD")){
//fragment transaction to go to the first screen
}
}
- 像
.addToBackStack(null)第二個片段一樣將空值推送到后堆疊,然后從第三個片段正常彈出
uj5u.com熱心網友回復:
你可以使用導航組件庫來解決這個問題!
這將是您的 nav_graph.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/nav_graph"
app:startDestination="@id/AFragment">
<fragment
android:id="@ id/AFragment"
android:name="ir.inbo.navigationComponenetBug.AFragment"
android:label="AFragment"
tools:layout="@layout/fragment_a">
<argument
android:name="someLong"
app:argType="long" />
<action
android:id="@ id/action_AFragment_to_BFragment"
app:destination="@id/BFragment" />
</fragment>
<fragment
android:id="@ id/BFragment"
android:name="ir.inbo.navigationComponenetBug.BFragment"
android:label="BFragment"
tools:layout="@layout/fragment_b">
<argument
android:name="someLong"
app:argType="long" />
<action
android:id="@ id/action_BFragment_to_CFragment"
app:destination="@id/CFragment" />
</fragment>
<fragment
android:id="@ id/CFragment"
android:name="ir.inbo.navigationComponenetBug.CFragment"
android:label="CFragment"
tools:layout="@layout/fragment_c" >
<argument
android:name="someLong"
app:argType="long" />
<action
android:id="@ id/action_CFragment_to_AFragment"
app:destination="@id/AFragment"
app:popUpTo="@id/AFragment"
app:popUpToInclusive="true" />
</fragment>
</navigation>
并且在 AFragment 中,由于片段生命周期,當您從 AFragment 導航到 Bfragment 時,您必須將邏輯移至 onViewCreated 或 onCreateView,Afragment 的視圖將被銷毀,當您從 Cfragment 回傳 Afragment 時,將呼叫 onViewCreated 和 onViewCreate
而這是給你的確切使用情況下例如谷歌
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/402050.html
標籤:安卓 分段 android-fragment-manager
