5月 152020
今天看到这样一个面试题,遂看了下Android系统源码
发现在ActivityThread.java中的performLaunchActivity函数中有这么一段代码
1 2 3 4 |
if (!r.activity.mFinished) { activity.performStart(); r.stopped = false; } |
可见,如果在onStart之前如果Activity执行了finish函数,就不会再执行onStart函数了。
示例代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
import androidx.appcompat.app.AppCompatActivity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.util.Log; import android.view.View; import static java.lang.ClassLoader.getSystemClassLoader; public class MainActivity extends AppCompatActivity { private String LOG_TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d(LOG_TAG, "onCreate"); finish(); } @Override protected void onStart() { Log.d(LOG_TAG, "onStart"); super.onStart(); } } |
Sorry, the comment form is closed at this time.