Williamz
Williamz

Reputation: 35

"getIntent()" - How it works

Learning how to make android apps and I did this tut. Summary of the tut is here:

http://sketchytech.blogspot.com/2012/10/android-simple-user-interface-activity.html

I'm trying to figure out how intents work. In the tut you create an Intent called intent, and in DisplayMessageActivity.java it creates an Intent called intent by calling "getIntent()".

Does the "getIntent()" function (or method (I'm most familiar with C)) just return the most recently created intent? Can there only be one intent at a time?

Thnks in advance for any responses!

Upvotes: 3

Views: 4620

Answers (4)

RMachnik
RMachnik

Reputation: 3684

There are two primary forms of intents you will use.

Explicit Intents have specified a component (via setComponent(ComponentName) or setClass(Context, Class)), which provides the exact class to be run. Often these will not include any other information, simply being a way for an application to launch various internal activities it has as the user interacts with the application. Implicit Intents have not specified a component; instead, they must include enough information for the system to determine which of the available components is best to run for that intent.

An Intent is a data class which holds information for an Activity that is about to be started. An Activity is a manager or controller for the view which is currently displayed on the screen.

Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

enter image description here

Upvotes: 0

SDJMcHattie
SDJMcHattie

Reputation: 1699

All Activities are started by either the startActivity(Intent) or the startActivityForResult(Intent, int) methods. The intent tells the Activity everything it needs to know to display the correct information at launch. getIntent(), when called in an Activity, gives you a reference to the Intent which was used to launch this Activity.

Upvotes: 2

Sebastiano
Sebastiano

Reputation: 12339

An Activity is typically created through an Intent. Assuming you are in your first activity:

Intent intent = new Intent(MyFancyActivity.class, Intent.ACTION_VIEW);
startActivity(intent);

This launches a new MyFancyActivity instance. From MyFancyActivity, you can retrieve the intent which lead to that instance creation. That is, the getIntent() method:

// this is the intent created in your first activity
Intent i = getIntent();

Upvotes: 0

Waqar Ahmed
Waqar Ahmed

Reputation: 5068

getIntent() method gets the intent that called this activity.there can be more than one intent but you have only one intent visible at one time (since only one activity is visible at one time)

Upvotes: 0

Related Questions