Android onRestart调用时机

onRestart调用时机

官方阐述:

onRestart

void onRestart ()

Called after onStop() when the current activity is being re-displayed to the user (the user has navigated back to it). It will be followed by onStart() and then onResume().
For activities that are using raw Cursor objects (instead of creating them through managedQuery(android.net.Uri, String[], String, String[], String), this is usually the place where the cursor should be requeried (because you had deactivated it in onStop().
Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.
If you override this method you must call through to the superclass implementation.

个人解读

官方解释强调再次显示activiy时,onRestart会被调用。再次显示有2种情况:

    1. activity由不可见变为可见
    1. 已经存在activity实例,再次启动activity时
      官方强调,如果复写该方法,应该在方法内调用父类方法,否则会抛出异常。
      所以正确的代码编写是:
@Override
protected void onRestart() {
    // If you override this method you must call through to the superclass implementation
    super.onRestart();
}



转自:https://www.jianshu.com/p/45e8af577e3f

发布了29 篇原创文章 · 获赞 49 · 访问量 6625

猜你喜欢

转载自blog.csdn.net/wangsen927/article/details/87261365