startActivityForResult案例

Info:startActivty 与 startActivityForResult区别

(1):startActivity 启动了其他Activity之后不会再回调过来,此时启动者与被启动者在启动后没有联系了。

(2):startActivityForResult 可以进行回调,之后有联系。

 

1:activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/tv_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1"
android:layout_below="@id/tv_show"/>
</RelativeLayout>

2:MainActivity.java

 1 public class MainActivity extends Activity {

 2     private Button btn1=null;

 3     private TextView tvShow=null;

 4     

 5     @Override

 6     protected void onCreate(Bundle savedInstanceState) {

 7         super.onCreate(savedInstanceState);

 8         setContentView(R.layout.activity_main);

 9         btn1=(Button)findViewById(R.id.btn_1);

10         

11         btn1.setOnClickListener(new OnClickListener(){

12             public void onClick(View view){

13                 Intent intent=new Intent(MainActivity.this,OtherActivity.class);

14                 startActivityForResult(intent,100);

15             }

16         });

17     }

18 

19     @Override

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

21         super.onActivityResult(requestCode, resultCode, data);

22         

23         if(requestCode==100 && resultCode==Activity.RESULT_OK){

24             String info=data.getExtras().getString("info");

25             tvShow=(TextView)findViewById(R.id.tv_show);

26             tvShow.setText(info);

27         }

28     }

29 }

 

3:activity_other.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/et_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="input info"/>

<Button
android:id="@+id/btn_back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button Back"
android:layout_below="@id/et_input"/>
</RelativeLayout>

4:OtherActivity.java

 1 public class OtherActivity extends Activity {

 2     private EditText etInfo=null;

 3     private Button btnBack=null;

 4     

 5     @Override

 6     protected void onCreate(Bundle savedInstanceState) {

 7         super.onCreate(savedInstanceState);

 8         setContentView(R.layout.activity_other);

 9         

10         etInfo=(EditText)findViewById(R.id.et_input);

11         btnBack=(Button)findViewById(R.id.btn_back);

12     

13         btnBack.setOnClickListener(new OnClickListener(){

14             public void onClick(View view){

15                 Intent intent=new Intent();

16                 String info=etInfo.getText().toString();

17                 intent.putExtra("info", info);

18                 

19                 setResult(Activity.RESULT_OK,intent);

20                 finish();    //关闭当前的Activity

21             }

22         });

23     }

24 }

 

你可能感兴趣的:(startActivityForResult案例)