从使用Intent调用的活动中获取结果

时间:2013-12-21 04:42:05

标签: android android-intent

我是Android开发的新手 我现在调用一个意图如何从被调用的活动中获得结果 谁能告诉我如何执行这项任务? 我称之为意图。

Intent I = new Intent (this ,abc.class); startActivity(i);

谢谢

4 个答案:

答案 0 :(得分:3)

使用startActivityForResult,然后在FirstActivity中覆盖onActivityResult

FirstActivity

Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
startActivityForResult(intent, 2);// Activity is started with requestCode 2  

覆盖onActivityResult

 @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");   
                            Log.i("Message is",message);
                            // logs Testing

                         }  

     }

SecondAcivity

Intent intent=new Intent();  
intent.putExtra("MESSAGE","Testing");  
setResult(2,intent);  
finish();//finishing activity  

参考文档:

http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,int)

示例:

http://www.javatpoint.com/android-startactivityforresult-example

答案 1 :(得分:0)

嗨,您可以获得结果通话活动

使用startActivityForResult(intent, 0);开始活动 在调用活动中添加以下方法

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {

            // do your task 

        } else if (resultCode == RESULT_CANCELED) {
            // do your task 
        }
    }

}

答案 2 :(得分:0)

在你的主要班级......

static final int CODE_REQUEST = 1;  // The request code

...

Intent pickContactIntent = new Intent(MainClass.this, CallingClassName.class);
startActivityForResult(pickContactIntent, CODE_REQUEST);

..........

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }
    }
}

在您的通话类中

Intent result = new Intent();
setResult(Activity.RESULT_OK, result);
finish()

答案 3 :(得分:0)

  
    
      

要进入第二个活动,请先使用startActivityForResult       类

             

Intent callIntent = new Intent(FirstClass.this,SecondClass.class);       startActivityForResult(callIntent,1);

             

然后覆盖你的第一个类中的onActivityResult方法,就像这样

    
         

protected void onActivityResult(int requestCode,int resultCode,Intent data){

         
// Check which request we're responding to
if (requestCode == 1) {
    // Make sure the request was successful
    if (resultCode == RESULT_OK) {


        // get values from data
    }
} }
    
         

在你的第二堂课中,如果你想发送,请回来     一流的东西。将此存储在您的意图中。

         

Intent result = new Intent(); setResult(Activity.RESULT_OK,result);     光洁度();