由 rememberSaveable 状态编写的 Android Jetpack Compose 在后退按钮中不存在

时间:2021-03-01 15:12:06

标签: android android-jetpack android-jetpack-compose android-jetpack-compose-runtime

我在 Restore UI state after activity or process recreationState and Jetpack Compose 部分中找不到代码示例中显示的 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script> <div class="row"> <div class="col-lg-9 mx-auto"> <!-- Accordion --> <div id="accordionExample" class="accordion shadow"> <!-- Accordion item 1 --> <div class="card"> <div id="headingOne" class="card-header bg-white shadow-sm border-0"> <h6 class="mb-0 font-weight-bold"><a href="#" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne" class="d-block position-relative text-dark text-uppercase collapsible-link py-2">Collapsible Group Item #1</a></h6> </div> <div id="collapseOne" aria-labelledby="headingOne" data-parent="#accordionExample" class="collapse show"> <div class="card-body p-5"> <p class="font-weight-light m-0">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.</p> </div> </div> </div> <!-- Accordion item 2 --> <div class="card"> <div id="headingTwo" class="card-header bg-white shadow-sm border-0"> <h6 class="mb-0 font-weight-bold"><a href="#" data-toggle="collapse" data-target="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo" class="d-block position-relative collapsed text-dark text-uppercase collapsible-link py-2">Collapsible Group Item #2</a></h6> </div> <div id="collapseTwo" aria-labelledby="headingTwo" data-parent="#accordionExample" class="collapse"> <div class="card-body p-5"> <p class="font-weight-light m-0">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.</p> </div> </div> </div> <!-- Accordion item 3 --> <div class="card"> <div id="headingThree" class="card-header bg-white shadow-sm border-0"> <h6 class="mb-0 font-weight-bold"><a href="#" data-toggle="collapse" data-target="#collapseThree" aria-expanded="false" aria-controls="collapseThree" class="d-block position-relative collapsed text-dark text-uppercase collapsible-link py-2">Collapsible Group Item #3</a></h6> </div> <div id="collapseThree" aria-labelledby="headingThree" data-parent="#accordionExample" class="collapse"> <div class="card-body p-5"> <p class="font-weight-light m-0">Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et.</p> </div> </div> </div> </div> </div> </div> 函数,该部分说“保留跨活动和流程重新创建的状态。”< /em>

我确实找到了包含 savedInstanceStateandroidx.compose.runtime.saveable documentation,这似乎是对 rememberSaveable 的重命名或替换,其文档还说“...存储的价值将在活动或过程娱乐中继续存在。”

然而,当我在我的代码中使用它时,状态不会在后退按钮中保留下来,尽管它在旋转中保留下来。这与文档所说的相反。

savedInstanceState

1 个答案:

答案 0 :(得分:3)

<块引用>

状态在后退按钮后不存在

在该示例中,这是预期行为。后退导航的默认行为是销毁活动。保存的实例状态此时被丢弃。这不是 Compose 独有的,并且自 Android 1.0 以来一直是 Android 的常见行为。

<块引用>

这与文档所说的相反。

文档中使用的“进程终止”指的是这个流程:

  • 用户在您的应用中
  • 用户切换到启动器(通过主页导航)或另一个应用(例如,通过概览屏幕)
  • 在接下来的几分钟内(但少于 30 分钟),Android 会在后台终止您的进程
  • 通过一种或另一种方式使用返回到您的应用

届时,Android 会为您创建一个新的进程,并尝试将您的 UI 恢复到用户离开您的应用之前的位置。保存的实例状态是恢复的一部分。