为什么我们不能使用new关键字创建活动? [英] why cant we create an activity using new keyword?

查看:145
本文介绍了为什么我们不能使用new关键字创建活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我们必须使用意图启动活动

why we have to launch an activity using intent

为什么我们不能使用Activity a = new Activity来启动它?

why cannot we launch it using Activity a = new Activity ??

我尝试在android开发人员中进行搜索,但没有得到任何答案

i tried searching in android developers but did not get any answer

推荐答案

可以,但是不会完全初始化.需要按正确的顺序调用Activity上的一系列函数以正确地对其进行初始化,并且框架为了显示Activity采取了一系列步骤.调用startActivity将采取这些步骤,并以正确的顺序调用Activity生命周期函数.因此,没有理由使用new.

You can, but it won't be fully initialized. There's a series of functions on the Activity that needs to be called in the right order to correctly initialize it, and a series of steps taken by the framework in order to display the activity. Calling startActivity will take those steps and call the Activity lifecycle functions in the correct order. So there's no reason to use new.

以另一种方式查看它-假设您要执行Activity a = new Activity().操作系统如何知道您这样做,并知道它应该显示该活动?它怎么知道该活动现在应该成为前台?不会的.需要告知的是,一个新的活动已经启动.这就是存在startAcctivity的原因.而且既然您仍然要调用它,为什么不将它也设置为工厂方法呢,那么初始化和启动就是一个步骤?

Looking at it another way- lets say you were to do Activity a = new Activity(). How would the OS know you did that, and know its supposed to display that Activity? How would it know that the Activity is now supposed to go into the foreground? It wouldn't. It needs to be told that a new Activity was launched. That's why startAcctivity exists. And since you have to call that anyway, why not just make it a factory method as well, so initialization and launching are one step?

这篇关于为什么我们不能使用new关键字创建活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆