我不会'理解为什么我们使用方法 getIntent()
。
因为,当我们需要这种方法时,我们可以使用方法 onActivityResult()
。
但是,通过使用 getIntent()
方法,可能会导致 NullPointerException
。
答案 0 :(得分:17)
http://developer.android.com/reference/android/app/Activity.html#getIntent()
返回开始此活动的意图。
如果您使用某些数据启动活动,例如执行
Intent intent = new Intent(context, SomeActivity.class);
intent.putExtra("someKey", someData);
您可以在新活动中使用getIntent检索此数据:
Intent intent = getIntent();
intent.getExtra("someKey") ...
所以,它不是处理来自Activity的返回数据,比如onActivityResult,而是用于将数据传递给新的Activity。
答案 1 :(得分:2)
getInent
用于将数据从活动传递到另一个活动,
例如,如果您要从名为startActivity
的活动切换到另一个名为endActivity
的活动,并且您希望在startActivity
中知道来自endActivity
的数据,那么您需要以下:
在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");
答案 2 :(得分:0)
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);
}
在“详细信息”活动中:
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String description = intent.getStringExtra("description");
String something_else = intent.getStringExtra("something_else");
现在使用字符串在所需位置显示值: 为
edittext.setText(name);
答案 3 :(得分:0)
实际上,当您要将数据从一页发送到另一页时,请使用get或put Intent
示例:
Intent intent = new Intent(context, HomeActivity.class);
intent.putExtra("yourData", yourData);
从以下位置检索数据
Intent intent = getIntent();
intent.getExtra("yourData")
答案 4 :(得分: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.
答案 5 :(得分:0)
将数据发送回同一个活动,getIntExtra() 在我的情况下工作:
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);
}
});
}