【问题标题】:Showing DialogFragment while Application is on background应用程序在后台时显示 DialogFragment
【发布时间】:2021-05-17 07:09:25
【问题描述】:

案例是在我收到异步任务的响应后,我想显示一个 DialogFragment。 但是,如果用户在应用程序仍在等待响应时将应用程序置于后台,则在响应到来的那一刻和 .show DialogFragment 它将崩溃。

我已通过尝试捕获 .show 立即修复,但用户返回应用后 DialogFragment 不会显示。

有没有一种干净的方法可以让应用程序在后台或下一个 onResume 时继续显示 DialogFragment?

我在谷歌搜索时发现的唯一方法是使用 ActivityDialog,但这需要付出很多努力。

编辑:嗯,我现在实际上可以使用 commitStateLoss ._ 来展示它。 从 customErrorDialog.show(((FragmentActivity) context).getSupportFragmentManager(), "TAG"); 到 ((FragmentActivity)context).getSupportFragmentManager().beginTransaction().add(customErrorDialog, "TAG").commitAllowingStateLoss();

idk 如果这对于某些特定情况很危险

【问题讨论】:

    标签: java android android-activity android-dialogfragment onpause


    【解决方案1】:

    您应该使用生命周期感知组件来接收响应。

    1. 对于 android-java 项目,只需使用 livedata。基于document:

    LiveData 具有生命周期感知能力,这意味着它尊重其他应用组件的生命周期,例如活动、片段或服务。这种意识确保 LiveData 只更新处于活动生命周期状态的应用组件观察者。 LiveData 认为观察者(由 Observer 类表示)如果其生命周期处于 STARTED 或 RESUMED 状态,则它处于活动状态。

    1. 对于 android-kotlin 项目,您有比 java 更多的选择。你仍然可以使用像 Java 这样的 livedata。其他选项是 StateFlow,它是流和协程的一部分。用 repeatOnLifecycle 收集它们。基于document

      public class NameActivity extends AppCompatActivity {
      
      private NameViewModel model;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          // Other code to setup the activity...
      
          // Get the ViewModel.
          model = new ViewModelProvider(this).get(NameViewModel.class);
      
          // Create the observer which updates the UI.
          final Observer<String> nameObserver = new Observer<String>() {
              @Override
              public void onChanged(@Nullable final String newName) {
                  // Update the UI, in this case, a TextView.
                  nameTextView.setText(newName);
              }
          };
      
          // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
          model.getCurrentName().observe(this, nameObserver);
      }
      }
      

    StateFlow 和 SharedFlow 是 Fl​​ow API,它们使流能够以最佳方式发出状态更新并向多个消费者发出值。

    您可以在 LiveData 等其他可观察类中找到这种行为

    class LatestNewsActivity : AppCompatActivity() {
    private val latestNewsViewModel = // getViewModel()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        // Start a coroutine in the lifecycle scope
        lifecycleScope.launch {
            // repeatOnLifecycle launches the block in a new coroutine every time the
            // lifecycle is in the STARTED state (or above) and cancels it when it's STOPPED.
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                // Trigger the flow and start listening for values.
                // Note that this happens when lifecycle is STARTED and stops
                // collecting when the lifecycle is STOPPED
                latestNewsViewModel.uiState.collect { uiState ->
                    // New value received
                    when (uiState) {
                        is LatestNewsUiState.Success -> showFavoriteNews(uiState.news)
                        is LatestNewsUiState.Error -> showError(uiState.exception)
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-08
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      相关资源
      最近更新 更多