0

例如,在监听 SearchDialog 关闭的活动中,您在 onCreate() 中注册监听器:

android.app.SearchManager searchManager =     (android.app.SearchManager)getSystemService(Context.SEARCH_SERVICE);
    if (searchManager != null)
    {
       searchManager.setOnDismissListener(this);
    }

我的问题是这将保持一个监听器还是我需要在说 resume() 中再次将它设置为监听器?或一个注册总是注册。我没有保留对 SearchManager 的本地引用。

还有我们为什么要这样做,是否有任何理由检查经理是否像这样为空,或者可以安全地假设您永远不会让空经理回来?

4

1 回答 1

0
  1. The listener will persist across the entire Activity's lifecycle, so you should set the listener in onCreate. Setting the listener in onResume is redundant and unnecessary.

  2. The Android documentation states that getSystemService will return null only if the service name (in this case Context.SEARCH_SERVICE) does not exist. So as long as you are positive it exists, it won't return null.

于 2012-06-23T00:20:34.393 回答