安卓:我们什么时候使用getIntent()?

16

我不明白为什么我们要使用getIntent()方法。

因为当我们需要该方法时,我们可以使用onActivityResult()方法。

但是通过使用getIntent()方法,可能会导致NullPointerException


你是指 Activity#getIntent() 吗? - DeeV
6个回答

36

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

返回启动此活动的意图。

如果您使用某些数据启动了 Activity,例如通过执行

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

您可以使用新活动中的 getIntent 方法检索此数据:

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

因此,它不是用于处理来自活动的返回数据,例如onActivityResult,而是用于向新活动传递数据。


3
我该把getIntent()放在哪里呢?在onStart还是onCreate中?还是Activity的主线程中? - ki9
无论何时何地,您都可以使用它。通常在onCreate中使用一次以提取所需数据。但是,如果数据的使用基于某些用户交互或其他情况,则也可以在任何其他地方使用它。 - Kenneth
4
答案需要进行一些修订,因为getExtra方法已被其他变体替代,例如getStringExtragetExtras(不确定getExtra是否曾经存在)。 - user2711811

5

getIntent用于在活动之间传递数据,例如,如果您想从一个名为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);

关于 endActivity

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

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);
        }

在你的详情活动中:
Intent intent = getIntent();
    String name = intent.getStringExtra("name");
    String description = intent.getStringExtra("description");
    String something_else = intent.getStringExtra("something_else");

现在使用字符串将值显示在所需的位置:如下

edittext.setText(name);


1
实际上,当您想要从一个页面发送数据到另一个页面时,可以使用get或put Intent。
示例:
Intent intent = new Intent(context, HomeActivity.class);
intent.putExtra("yourData", yourData);

从中检索数据。
Intent intent = getIntent();
intent.getExtra("yourData")   

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);
         }
     });
 }

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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接