• 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 }
  • 相关阅读:
    android细节之禁用activity的系统的默认切换效果
    Spark1.0.0 属性配置
    Memory & MyISAM 引擎小注意! | OurMySQL
    memcached vs MySQL Memory engine table 速度比较_XMPP Jabber即时通讯开发实践_百度空间
    Mysql 官方Memcached 插件初步试用感受
    Aerospike | Aerospike Chinese
    MySQL内存表的特性与使用介绍 -- 简明现代魔法
    memory引擎的索引失效一例
    MySQL内存表(MEMORY)说明 | 一个PHP程序员的备忘录
    MySQL Memory 存储引擎浅析
  • 原文地址:https://www.cnblogs.com/yshyee/p/3361386.html
Copyright © 2020-2023  润新知