Android : When do we use getIntent()?
Asked Answered
H

6

16

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.

Hui answered 3/11, 2014 at 19:53 Comment(1)
Do you mean Activity#getIntent()?Leftward
T
36

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.

Tufted answered 3/11, 2014 at 19:58 Comment(3)
Wondering where I should put getIntent(). In onStart? onCreate? Activity main thread?Mesnalty
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.Tufted
Answer needs some revision as the getExtra method is replaced by other varieties such as getStringExtra or getExtras (not sure getExtra ever existed).Homozygous
C
5

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");
Contrasty answered 3/6, 2017 at 13:9 Comment(0)
B
1
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);

Bushcraft answered 27/8, 2018 at 4:35 Comment(0)
T
1

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")   
Theobald answered 4/10, 2019 at 9:42 Comment(0)
B
0
  //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.
Brendin answered 20/11, 2019 at 13:51 Comment(0)
U
0

Send data back to same Activity, getIntExtra() work in my case:

 protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     ...

     Button reset_btn = (Button) findViewById(R.id.reset);

     reset_btn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) { 
             Intent intent= getIntent();               
             int counter = intent.getIntExtra("Num", 0);//"Num" is the key, 0 is an initial value
             counter = counter + 1;
             Log.d("number of reset >>>>>","counter= " + String.valueOf(counter));
             intent.putExtra("Num", counter);
             finish();
             startActivity(intent);
         }
     });
 }
Ursa answered 19/3, 2021 at 10:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.