执行finish()时捆绑或PutExtra

时间:2016-11-02 03:55:07

标签: android

我想问一下在调用Extras方法时是否有办法放Bundlefinish()

例如,就像调用Activity一样,你可以这样做:

Intent intent = new Intent(view.getContext(), AddListing.class);
intent.putExtra("user_id", userID);
startActivity(intent);

是否可以在finish()

中使用它

感谢。

3 个答案:

答案 0 :(得分:4)

如果您希望在Activity被销毁时传回结果,那么您应该寻找的是: startActivityForResult

startActivityForResult()有两种变体,允许在必要时传递bundle

public void startActivityForResult (Intent intent, int requestCode)  
public void startActivityForResult (Intent intent, int requestCode, Bundle options)  

请参阅此示例以更好地理解该概念:

<强> MainActivity.java:

     protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        textView1=(TextView)findViewById(R.id.textView1);  
        button1=(Button)findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View arg0) {  
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);  
                startActivityForResult(intent, 2);// Activity is started with requestCode 2  
            }  
        });  
    }  
     // Call Back method  to get the Message from other Activity  
    @Override  
       protected void onActivityResult(int requestCode, int resultCode, Intent data)  
       {  
                 super.onActivityResult(requestCode, resultCode, data);  
                  // check if the request code is same as what is passed  here it is 2  
                   if(requestCode==2)  
                         {  
                            String message=data.getStringExtra("MESSAGE");   
                            textView1.setText(message);  
                         }  
     }  

<强> SecondActivity.java:

protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_second);  
    editText1=(EditText)findViewById(R.id.editText1);  
        button1=(Button)findViewById(R.id.button1);  
        button1.setOnClickListener(new OnClickListener() {  
            @Override  
            public void onClick(View arg0) {  
                String message=editText1.getText().toString();  
                Intent intent=new Intent();  
                intent.putExtra("MESSAGE",message);  
                setResult(2,intent);  
                finish();//finishing activity  
            }  
        });  
}  

答案 1 :(得分:0)

没有finish()方法可以使用intent作为参数。如果您想将某些数据传递回调用Activity,则可以在调用setResult(int resultCode, Intent intent)之前使用finish()。在此方法中,您需要在intent中传递bundle或extras。并在调用activity的onActivityResult方法中接受它。这是将数据传递回Activity的唯一方法。您可以参考此链接https://developer.android.com/training/basics/intents/result.html

答案 2 :(得分:0)

Define the userId-->after the intent initiation ,

Example,
 Intent i = new Intent(1st.this, 2nd.class);
                    String sDate = Dpcheckin.getText().toString();
                    String eDate = Dpcheckout.getText().toString();
                    String roomtext=RoomText.getText().toString();
                    i.putExtra("date1", sDate);
                    i.putExtra("date2", eDate);
                    i.putExtra("roomtext",roomtext);
                    startActivity(i);

其中, 主要是你需要在Intent中定义2个类, 第一个是您发送数据的类,具有(.this)扩展名。 第二个是您接收数据的类,扩展名为(.class)。