Reputation: 2130
I was asked the following question in an interview :
Why do we need Intents to start new Activity or Service? Alternatively, why don't we start Activity by creating a new object and calling activity_obj.onCreate(..); on it.
I could count the benefits of intents like transferring data and intent-filters targeting appropriate activities, but except that I couldn't come up with a satisfying answer.
Is it related to life cycle callback handling of Activities ?
Upvotes: 0
Views: 159
Reputation: 4086
The way Android starts Activities is through Zygotes and a whole lot of Java configuration. I feel like it would be a big hot mess if we were to do all the dirty work configuring and starting activities. This way with intents we can make "system-calls" which just hand over to Android for a bit and ask it to do some stuff.
From a security point of view, it also means that each Activity is started by Android. Which means you can't start Whatsapp, to share some text, and have full root control over it because you started it. This is very beneficial, and Android even provides a way for you to interact with your own Activities if needs be by communicating within the same process. This Sandboxed approach is one of the elements that helps the platform's security.
Upvotes: 5
Reputation: 95578
The main reason is that this gives Android an easy way to fully customize the user experience. By the use of "implicit Intents", your application can tell Android that it should find a component that is capable of performing a certain type of ACTION on a certain type of data (for example, "VIEW an URL" or "OPEN a PDF-Document" or "COMPOSE an EMAIL"). Your app doesn't need to know what component to launch to do this. In fact, the user may have several components installed on his device that can perform the desired ACTION, so this allows Android to ask the user what component should be used to perform the desired ACTION. This makes it incredibly easy to provide drop-in replacements for almost all the apps on your device.
Of course there are 25 other ways to accomplish this. The original developers chose the Intent
mechanism.
Upvotes: 0