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
.
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.
getIntent()
. In onStart? onCreate? Activity main thread? –
Mesnalty getExtra
method is replaced by other varieties such as getStringExtra
or getExtras
(not sure getExtra
ever existed). –
Homozygous 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");
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);
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")
//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.
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);
}
});
}
© 2022 - 2024 — McMap. All rights reserved.
Activity#getIntent()
? – Leftward