简体   繁体   English

当我们在 onCreate() 方法中调用 finish() 时会发生什么?

[英]What happens when we call finish() in onCreate() method?

What happens when we call the finish() method for the activity inside onCreate() ?当我们为onCreate()中的活动调用finish()方法时会发生什么? Will the app get time to display the Activity , but then rapidly close, or won't it show anything?该应用程序是否有时间显示Activity ,但随后迅速关闭,或者它不会显示任何内容? This is just a doubt that I had before reading about some malware services for android that allegedly use this idea... Does it effectively work?这只是我在阅读一些据称使用这个想法的 android 恶意软件服务之前的疑问......它有效吗?

I just tested and debugged it, the Activity starts but is immediately closes once the event is generated.我刚刚对其进行了测试和调试, Activity启动但在事件生成后立即关闭。

As MrMins mention it will rapidly close.正如 MrMins 提到的那样,它将迅速关闭。 The lifecycle will skip over some important steps, therefore causing potential issues.生命周期会跳过一些重要的步骤,从而导致潜在的问题。

But to answer you question it will run only momentarily.但要回答你的问题,它只会暂时运行。

Here is the android documentation of the lifecycle: https://developer.android.com/guide/components/activities/activity-lifecycle.html这是生命周期的 android 文档: https://developer.android.com/guide/components/activities/activity-lifecycle.html

As per official documentation : 根据官方文件

You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.您可以从这个 function 中调用 finish(),在这种情况下,onDestroy() 将在 onCreate(Bundle) 之后立即被调用,而没有任何活动生命周期的 rest(onStart()、onResume()、onPause() 等)执行。

Since 自从

The visible lifetime of an activity happens between a call to onStart() until a corresponding call to onStop().活动的可见生命周期发生在对 onStart() 的调用与对 onStop() 的相应调用之间。

so the "visible lifetime" is not reached for that case.所以这种情况下没有达到“可见寿命”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我们明确调用Garbage collector方法时会发生什么? - What happens when we explicitly call Garbage collector method? 当我们调用ArrayList.add()方法时会发生什么? - What happens when we call ArrayList.add() method? 当我们使用类实例调用静态方法/函数时会发生什么? - What happens when we call a static method/function using class instance? 当我们在同一方法上添加@GET和@Consumes时会发生什么 - What happens when we add @GET and @Consumes on the same method 当少数线程尝试调用相同的同步方法时会发生什么? - What happens when few threads trying to call the same synchronized method? 在已经工作的线程中调用方法时会发生什么? - What happens when you call a method in a thread that is already working? java-当我在构造函数中调用方法时会发生什么? - java-What happens when I call a method in constructor? 首先会发生什么:onCreate 或 configureFlutterEngine? - What happens first: onCreate or configureFlutterEngine? 当我们提交时到底发生了什么? - What happens exactly when we commit? 了解我们在调用super.clone和不调用super.clone时覆盖clone方法时会发生什么情况? - Understanding what happens when we override the clone method with and without invoking super.clone?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM