Android : When do we use getIntent()?

android intent
getextra android
onstart android
android getintent deprecated
intent putextra object
how to get data from bundle in android
setresult android
android activity get intent data

I dont' understand why we use method getIntent().

Because, when we need that method, We can use method onActivityResult().

But by using the method getIntent(), it could cause NullPointerException.

http://developer.android.com/reference/android/app/Activity.html#getIntent()

Return the intent that started this activity.

If you start an Activity with some data, for example by doing

Intent intent = new Intent(context, SomeActivity.class);
intent.putExtra("someKey", someData);

you can retrieve this data using getIntent in the new activity:

Intent intent = getIntent();
intent.getExtra("someKey") ...

So, it's not for handling returning data from an Activity, like onActivityResult, but it's for passing data to a new Activity.

Java Code Examples android.app.Activity.getIntent, You can vote up the examples you like. Your votes will be used in our system to get more good examples. Example 1. Project  Android Intent Tutorial. Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc. It is generally used with startActivity() method to invoke activity, broadcast receivers etc. The dictionary meaning of intent is intention or purpose. So, it can be described as the

getInent is used to pass data from an activity to another, For example If you want to switch from an activity named startActivity to another one named endActivity and you want that a data from startActivity will be known in the endActivity you do the following:

In startActivity:

String dataToTransmit="this info text will be valid on endActivity";
Intent intent =new Intent(this, endActivity.class);
intent.putExtra("dataToTransmitKey",dataToTransmit);
startActivity(intent);

on endActivity:

Intent intent = getIntent();
String dataTransmited=intent.getStringExtra("dataToTransmitKey");

Intent.putextra – How to use Intents & Extras to pass data between , Intents in android offer this convenient way to pass data between activities We can start adding data into the Intent object, we use the method defined getIntent();; //get the attached extras from the intent; //we should use the  An application can access other Android components to achieve a task. For example, from a component of your application you can trigger another component in the Android system, which manages photos, even if this component is not part of your application. In this component you select a photo and return to your application to use the selected photo.

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            Intent intent = new Intent(ItemListView.this, ViewItemClicked.class);
            String name = itemList.get(position).getString(1);
            String description = itemList.get(position).getString(2);
            String something_else = itemList.get(position).getString(3);
            intent.putExtra("name", name);
            intent.putExtra("description", description);
            intent.putExtra("something_else", something_else);
            startActivity(intent);
        }

In your Details Activity:

Intent intent = getIntent();
    String name = intent.getStringExtra("name");
    String description = intent.getStringExtra("description");
    String something_else = intent.getStringExtra("something_else");

Now use the strings to show the values in the desired places: as

edittext.setText(name);

android.app.Activity.getIntent java code examples, How to use. getIntent. method. in. android.app.Activity Activity a = thenReturn​(originalIntent); // Setup the data the app will send back to messenger. getInent is used to pass data from an activity to another, For example If you want to switch from an activity named startActivity to another one named endActivity and you want that a data from startActivity will be known in the endActivity you do the following:

Actually when you want to send some data from one page to another page then use get or put Intent

example:

Intent intent = new Intent(context, HomeActivity.class);
intent.putExtra("yourData", yourData);

Retrieve data from

Intent intent = getIntent();
intent.getExtra("yourData")   

Activity, Click on login button and we will see the message in Toast. statements which we study in detail: ○ First when we want to send data from one activity to another we use Bundle class object. For this we use getIntent and getExtras() method. We use the ID to get a reference to the TextView in the java file. Go to that java file now — SecondActivity.java — and add this code to the onCreate method: String savedExtra = getIntent().getStringExtra("value1"); TextView myText = (TextView) findViewById(R.id.textID); myText.setText(savedExtra); placed like:

  //Sending data...
  //creating and initializing an Intent object
Intent intent = new Intent(this, NextActivity.class);

  //attach the key value pair using putExtra to this intent
String user_name = "Jhon Doe";
intent.putExtra("USER_NAME", user_name);

  //starting the activity
startActivity(intent);


  //Retrieving data from intent
  //get the current intent
Intent intent = getIntent();

  //get the attached extras from the intent
  //we should use the same key as we used to attach the data.
String user_name = intent.getStringExtra("USER_NAME");

  //if you have used any other type of data, you should use the 
  //particular getExtra method to extract the data from Intet
Integer user_id = intent.getIntExtra("USER_ID");
float user_rating = intent.getFloatExtra("USER_RATING");

Note: you should specify type of that before sending the value.

Starting with Android: Android application development guide, It can be used with Context#startActivity(Intent) to launch an android.app.Activity Android Learn more Android : When do we use getIntent()?. Activity, This  An activity has a button that takes the user to another activity. To launch the new activity, we populate our Intent with extras, and onCreate(), the new activity reads from those extras via Intent.getExtras(). We assumed the returned bundle would be non-null, but as customer crash reports discovered, getExtras() sometimes returns null.

MainActivity puts valid String into Extras for an Intent but the Intent , allow multiple applications on Android to use one another. These alternative menus are part of the Android interapplication communication or usage framework. If the result is null, do not use the intent and, if possible, you should disable the feature that issues the intent. The following example shows how to verify that the intent resolves to an activity. This example doesn't use a URI, but the intent's data type is declared to specify the content carried by the extras.

Pro Android 3, The invoked activity will then use the data URL from the intent that is passed. To understand alternative menus well, you must first understand content providers, called getIntent() that returns the data URI for which this activity is invoked.

Pro Android 2, I am new to Android development and I have come across something new while using Intent as getIntent().getExtras().Can anyone please 

Comments
  • Do you mean Activity#getIntent()?
  • Wondering where I should put getIntent(). In onStart? onCreate? Activity main thread?
  • Whereever and whenever you need it, often used once in onCreate to extract the data needed. But could also be used anywhere else, if the use of the data is based on some user interaction or whatever.
  • Answer needs some revision as the getExtra method is replaced by other varieties such as getStringExtra or getExtras (not sure getExtra ever existed).