簡體   English   中英

使用正確的片段和狀態恢復活動

[英]Restore activity with the right fragment and state

我有一個帶有主菜單的應用程序,單擊該主菜單可導航到其他活動。

這些活動之一是“搜索”,帶有我更新了很多片段的容器。 它是一種搜索,只需3個步驟。 (步驟1,步驟2,步驟3)

當我進行其他活動並返回到搜索活動時,狀態會丟失。

用正確的片段恢復活動的最佳方法是什么?

我嘗試過android:launchMode =“ singleInstance” ,也嘗試過intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT或Intent.FLAG_ACTIVITY_CLEAR_TOP)強制執行一個活動實例,並且確實可以正常工作,但第一次打開該活動需要時間(應用種類)停止工作,然后重新開始)

我想我不能只使用savedInstanceState(),因為每個片段都需要一些在上一個片段上選擇的信息

有什么建議嗎?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">

    <!--Top bar Menu-->
    <include  layout="@layout/partials_top_bar"/>


    <FrameLayout
        android:windowSoftInputMode="adjustNothing"
        android:id="@+id/container"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </FrameLayout>


    <!--Bottom Bar Menu-->
    <include  layout="@layout/partials_bottom_bar"/>

</LinearLayout>

當用戶選擇某些操作時,我將使用

        activity.supportFragmentManager
                .beginTransaction()
                .replace(R.id.container, fragment)
                .addToBackStack(null)
                .commit()

編輯:找到了解決方案。 我打開SingleInstance時顯示的“延遲” /應用程序凍結是由於刪除所有活動的過渡動畫引起的。 但是只有單實例活動會出現此延遲。 所以我要用這個。 十分感謝大家。

您可以在活動中創建一個靜態變量“ state”,可以是String,並且當您更改Fragment時,您可以更改變量值,例如,當您更改為StepOneFragment時,請將其值設置為“ step_one”,StepTwoFragment-> “ step_two”,StepThreeFragment->“ step_three”,如果返回,則根據您所處的片段來更改變量值,然后在SearchActivity的onCreate中,對該變量進行切換,並根據值進行切換加載所需的片段。

希望能幫助到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM