android 從主activity傳值到子activity再把結果返回到主界面的示例

在原文檔中是:Start Activity and Getting Resultsandroid

The startActivity(android.content.Intent) method is used to start a new activity, which will be placed at the top of the activity stack. It takes a single argument, an Intent, which describes the activity to be executed.

Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) 
用startActivityForResult(Intent, int) 方法替代了原來的startActivity方法。
version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, android.content.Intent) method.

When an activity exits, it can call setResult(int) to return data back to its parent. It must always supply a result code, which can be the standard results RESULT_CANCELED, RESULT_OK, or any custom values starting at RESULT_FIRST_USER. In addition, it can optionally return back an Intent containing any additional data it wants. All of this information appears back on the parent's Activity.onActivityResult(), 
在主Activity中重寫onActivity方法來獲得返回的結果!
along with the integer identifier it originally supplied.

If a child activity fails for any reason (such as crashing), the parent activity will receive a result with the code RESULT_CANCELED.

 public class MyActivity extends Activity {
     ...

     static final int PICK_CONTACT_REQUEST = 0;

     protected boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             // When the user center presses, let them pick a contact.
             startActivityForResult(
                 new Intent(Intent.ACTION_PICK,
                 new Uri("content://contacts")),
                 PICK_CONTACT_REQUEST);
            return true;
         }
         return false;
     }

     protected void onActivityResult(int requestCode, int resultCode,
             Intent data) {
         if (requestCode == PICK_CONTACT_REQUEST) {
             if (resultCode == RESULT_OK) {
                 // A contact was picked.  Here we will just display it
                 // to the user.
                 startActivity(new Intent(Intent.ACTION_VIEW, data));
             }
         }
     }
 }

在主activity中app

重點在於用startActivityForResult(Intent, requestCode) 方法替代了原來的startActivity方法。ide

且多加了:onActivityResult(int requestCode, int resultCode, Intent data) 方法獲得子activity返回的結果post

本身的程序:this

 1 package com.example.saagr;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.content.Intent;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.widget.Button;
10 import android.widget.EditText;
11 import android.widget.TextView;
12 
13 public class MainActivity extends Activity {
14     private EditText editText, editText2, editText3;
15     private Button button;
16     private TextView textView, textView2;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         button = (Button) findViewById(R.id.but_post);
23         editText = (EditText) findViewById(R.id.et1);
24         editText2 = (EditText) findViewById(R.id.et2);
25         editText3 = (EditText) findViewById(R.id.et3);
26         textView = (TextView) findViewById(R.id.tv_add);
27         textView2 = (TextView) findViewById(R.id.tv_equ);
28         button.setOnClickListener(new OnClickListener() {
29 
30             @Override
31             public void onClick(View v) {
32                 // TODO Auto-generated method stub
33                 String s1 = editText.getText().toString();
34                 String s2 = editText2.getText().toString();
35                 Intent intent = new Intent(MainActivity.this, SubActivity.class);
36                 intent.putExtra("message", s1 + "+" + s2 + "=" + "?");
37                 startActivityForResult(intent, 1000);
38 
39             }
40         });
41 
42     }
43 
44     @Override
45     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
46         // TODO Auto-generated method stub
47         super.onActivityResult(requestCode, resultCode, data);
48         if (requestCode == 1000 && resultCode == 1001) {
49             String result = data.getStringExtra("result");
50             editText3.setText(result);
51         }
52     }
53 
54     @Override
55     public boolean onCreateOptionsMenu(Menu menu) {
56         // Inflate the menu; this adds items to the action bar if it is present.
57         getMenuInflater().inflate(R.menu.main, menu);
58         return true;
59     }
60 
61 }
View Code

 

在子activity中spa

用setResult(int resultCode Intent data)方法給主activity返回結果3d

本身的程序:code

package com.example.saagr;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SubActivity extends Activity {
    private TextView textView;
    private EditText editText;
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        textView = (TextView) findViewById(R.id.tv_show);
        editText = (EditText) findViewById(R.id.et);
        button = (Button) findViewById(R.id.but);
        Intent intent = getIntent();
        String message = intent.getStringExtra("message");
        textView.setText(message);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String result = editText.getText().toString();
                Intent intent = new Intent();
                intent.putExtra("result", result);
                setResult(1001, intent);
                finish();

            }
        });

    }

}
View Code

總結:orm

主activity用startActivityForResult(Intent, requestCode) 方法開啓子activity而且會傳過去一個requestCode用來標識;blog

子activity用getIntent()獲得發過來的數據並進行處理,再將結果數據封裝成一個intent用setResult(int resultCode, Intent intent)返回給主activity(resultCode也是用來標識的);

主activity再用onActivityResult(int requestCode, int resultCode, Intent data) 方法獲得返回的結果而且在此方法體中會定義判斷語句來判斷requestCode和resultCode是否和相應的標識相符,再對結果進行處理

相關文章
相關標籤/搜索