Why use an intent to create an activity? - android

Folks,
This is a newbie question. I have read a few articles on intents but I am a bit confused on what the main idea behind an intent is when it comes to starting an activity. If I know that I have to create and show an activity, why can't I do something as simple as the following?
MyActivity a = new MyActivity();
a.show();
Thank you in advance for your help.
Regards,
Peter

A activity has a lifecycle and this is managed by the framework. I would say to get an extra hold of the life cycle, Android introduced a set of rules to launch a activity (startActivity). To add-on, Intent is not only to launch your activities. Intents can be used to launch other thirdparty or inbuild views/service/targets. This could be another reason why they introduced intent. Just my two cents.

Intents communicate between activities in an app and between apps.
Your example:
MyActivity a = new MyActivity();
a.show();
assumes that Android is just objects, so that instantiating an Activity and somehow showing it will make it appear. This isn't the case, though; the Android system does a lot more. The activities in your app are "floating", as it were, within the Android framework. Most of what makes an Activity tick is invisible to you. In particular, the Android-specific thread model and the way that the system communicates with Android components (like Activities) is invisible.
I won't go into most of this, but an added advantage of Intents for starting an Activity is that an Activity can add itself as a candidate for Intents that want to do a specific task. Suppose I have an app that edits images. I can easily make myself discoverable by filtering for Intents that have the action ACTION_EDIT for MIME types that I can handle. This is exactly how Android implements the list of apps that appear when you try do to something with a file.

An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.
click here for more information.

in basic words Intent is your ears regards to Android device. Your activity can talk to Android through Intent and listen on "any change" on your phone.
It's like "grandmother" that sits outdoors next to entrance of big house and listens about all talks, gossips and notify you about.

Related

how to call certain method of activity via appwidget

i'm trying to call a method in my main activity via a button of a widget.
the widget has different buttons with different values. i want the method to do its job without showing up the gui attached to the main activity. (send http request with button value)
so far i searched for some tutorials but i didn't quite understand them, my code got all clumsy and it barely worked. i think a mix of intents, services and broadcasts are needed to realize this?! i really don't know, can somebody post a understandable description or tutorial which covers all aspects of how to do this?
i think there is no code or pictures to provide because there is nothing look at right now.
thanks in advance for any answer.
Android AppWidgets don't need to have any relation at all to an Activity, although it is certainly possible to have a click or something start an activity.
As you probably know, the primary interface to AppWidgets is through your AppWidgetProvider (if you don't know what any of this is about, take a look at the official android guide).
You can tell a button to use a PendingIntent to do one of several things when clicked:
To start a service (probably your best option for time consuming tasks like http requests), using PendingIntent.getService.
To start an activity, use PendingIntent.getActivity.
To broadcast a message to a BroadcastReceiver, create it using PendingIntent.getBroadcast.
In all of these cases, you would tell the button to run the PendingIntent with the
setOnClickPendingIntent(int id, PendingIntent operation) method of RemoteViews.
The Intent you pass to any of these methods should typically be created using the Intent(Context, Class) constructor, where the second argument is the service, activity or receiver class you want the pending intent to be sent to.

Use of intents in android

I am new to android and am currently reading a book called 'Professional.Android.2.Application.Development'.
I have seen some of the code given to me to start an application development and I have noticed that most of the java code consists of intents.
I want to know why we are using intents and not events or services.
All I know about intents are that they are an abstract description of an operation to be performed.
I also want to know the importance of intents in android.
Can anyone please explain it to me as I am new and a little bit confused.
What is a Intent ?
Intent is basically a message that is passed between components (such as Activities, Services, Broadcast Receivers, and Content Providers). So, it is almost equivalent to parameters passed to API calls. The fundamental differences between API calls and intents’ way of invoking components are:
API calls are synchronous while intent-based invocations are
asynchronous.
API calls are compile time binding while intent-based calls are
run-time binding.
Of course, Intents can be made to work exactly like API calls by using what are called explicit intents, which will be explained later. But more often than not, implicit intents are the way to go and that is what is explained here.
One component that wants to invoke another has to only express its’ intent to do a job. And any other component that exists and has claimed that it can do such a job through intent-filters, is invoked by the android platform to accomplish the job. This means, both the components are not aware of each other’s existence and can still work together to give the desired result for the end-user.
This invisible connection between components is achieved through the combination of intents, intent-filters and the android platform.
This leads to huge possibilities like:
Mix and match or rather plug and play of components at runtime.
Replacing the inbuilt android applications with custom developed
applications.
Component level reuse within and across applications.
Service orientation to the most granular level, if I may say.
Here is additional description about intent, almost formal.
An intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.
An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed. The primary pieces of information in an intent are:
action
The general action to be performed, such as ACTION_VIEW,
ACTION_EDIT, ACTION_MAIN, etc.
data
The data to operate on, such as a person record in the contacts
database, expressed as a Uri.
On this data structure is that the android is implemented as you read the following documentation is very helpful:
Android Intent first documentation
Android Intent second documentation
Android Intent third documentation
Android Intent Native documentation

android intent vs observer

I am wondering when I should favour an intent based approach over a classic oberserver design pattern in my Android app.
Would it be wize to use intent's to publish events within one activity?
Definition
Observer maintain a list of dependents to notify a change. On the other side Intent is an abstract description of operation. Which can be received by same or multiple application to perform a task.
Synchronous VS Asynchronous
Observer is synchronous as the changes effect right-way. Intent is asynchronous.
Binding
Observer bindings are compile time. On the other side intent bindings are at run time
Secure
For internal communication observer is much secure as Intent will broadcast message and anyone can receive the message.
Intent provide additional functionality over Observer on Android platform
Communicating with third party application and processes.
Receive system notification to perform a task
Application module reuse by third party app. (Ex taking pic)
Intent stays around and your app can retrieve data in some later time (Sticky Intent)
Would it be wise to use intent's to publish events within one activity?
This will depend on your requirement. Look into your requirement to see which one needed.
I think not because a new intent will usually bring a new activity unless your activity has been declared as single top. Anyway intents do not resemble observers much. A Handler can be a better approximation.

Android App with multiple views - Best Practices?

I am new to developing for android. I have a question regarding some best practices. My app is like a dashboard from which multiple different "sub-activities" can be started and done.
I am wondering what is the best way to structure the app. One way is to have different layouts and load and unload them appropriately. The other is to start new activities using intents. At least this is what i have gathered from what i have read.
What in your opinion is the best way to go.
Thanks
I've found in my applications that each Activity is generally responsible for a single UI view.
So rather than loading and unloading different layouts, which can potentially get quite messy, it is better to separate each sub-activity into its own Activity class and use explicit intents (intents that name the target activity explicitly rather than relying on an intent filter) to move between them.
The decision you have to make is whether or not your activities should be tightly or loosely coupled. Loading and unloading the activity is typically appropriate from within your own app. Using intents is appropriate when you need to open an activity that you may or may not know the specifics of. For example, you would open another activity from your main menu (assuming you have one) directly. Then later, let's say you need to open up an address with a map, you would use an intent, because you don't really know the SPECIFIC activity to open. Secondly, using intents are best for when there are multiple activities that could do the same function, such as opening a URL in a browser.
So in summary:
Open Directly (Loading a new view or using Intent specifying the Component Name)
Tightly coupled
Know specifics of the Activity to load
Open Indirectly (Intent specifying the category of Activities that can handle it)
Don't necessarily know the specifics of the Activity beyond that it can perform some action that has been advertised.
There are multiple Activities that can perform the desired action, and you want the user to be able to choose for themselves which Activity to use.
While Intents may be a little extra work, I'd recommend using them, if you don't directly need to pass large blocks of data back and forth between the two.
If you just need to pass information TO each of the sub-programs, then you can easily do that with putExtra(String key, Bundle values);
By using intents, you spend a little time now in order to have a lot of flexibility later. You can start intents from different points, so you'd not need to write new code if one of your sub-applications wanted to start a different one, or you wanted a certain filetype opened with a file manager to open one of your sub-programs.

Communication between Activities: Intent or Service: what is faster?

Is there a significant difference in time needed for sending data over a service or by using an intent?
Are there general advices when to use service and when to use intents?
These are two completely different things. The question isn't which is faster, but what you are trying to do.
If you want to transfer data from one activity to another, you pass it through the intent. If this is not sufficient for you (too much data for example), you can take other approaches but they will not involve a Service. For example, you may have a singleton holding your shared data, which both activities access... but be extremely careful about your process being killed at various points which causes the singleton to go away (and using a Service for this won't let you get away with not dealing with such a situation).
A Service is to do some work in the background even if the user isn't directly interacting with the app. Especially if we are talking about stuff within one .apk (and thus typically one process), there are very few other reasons to use a Service.
It depends of what you need.
Intent is preferable if you can. You will be able to send primitives from an activity to an other, and using startActivityForResult() you'll get an intent back to the caller Activity.
Service is for data processing in the background and can be very CPU/Memory consuming. With a Service, you have to create an interface between your Activity and the Service, so you can call basic methods of the Service directly from the Activity, you can control the service from the Activity.
This is really not the same purpose. Read documentation about Intents and the information you can Bundle in it, that's probably what you need.
When you want to pass data from your current activity to a new activity, the best is to pass a Bundle along with your Intent. It is used to pass on "acquired" user data.
Services run in the background while another activity is still in the foreground. "Background" doesn't mean that it doesn't display - most services have a graphic visualisation of some sort - it means that it isn't part of the activities stack. For example, your Activity may be sending a text message and your Service may be a soft keyboard. Services can communicate with activities - in this instance, your keyboard of course needs to send the characters to the text message Activity - but it often involves using a rather complex interface. It is used for collecting and passing on "live" user data to an Activity.
Many methods to pass data between activities. See here for tips on a way to choose.

Categories

Resources