Android之数据传递

Android之数据传递,第1张

概述例子:主页面和子页面之间传值parent.xml<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:tools="http://schemas.android.comools"android:layou


例子:主页面和子页面之间传值
parent.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout 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"    tools:context=".MainActivity">    <button        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:onClick="parentClick"        androID:text="parent to child" /></linearLayout>

child.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout 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"    tools:context=".Activity_child">    <button        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:onClick="childClick"        androID:text="child to parent" /></linearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {    final static int RQC = 1;    final static int RSC = 1;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.parent);        Log.v("test", "MainActivity  onCreate()");    }    public voID parentClick(VIEw vIEw) {        Log.v("test", "MainActivity Click会调用startActivityForResult()");        Intent intent = new Intent(this, Activity_child.class);        startActivityForResult(intent, RQC);    }    @OverrIDe    protected voID onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {        super.onActivityResult(requestCode, resultCode, data);        if (requestCode == RQC) {            if (resultCode == RSC) {                String string = data.getStringExtra("extra_data");                Log.v("test", "获取的位置模拟数据为:" + string);            }        }    }    @OverrIDe    protected voID onStart() {        super.onStart();        Log.v("test", "MainActivity  onStart()方法");    }    @OverrIDe    protected voID onResume() {        super.onResume();        Log.v("test", "MainActivity  onResume()方法");    }    @OverrIDe    protected voID onRestart() {        super.onRestart();        Log.v("test", "MainActivity onRestart()方法");    }    @OverrIDe    protected voID onStop() {        super.onStop();        Log.v("test", "MainActivity onStop()方法");    }    @OverrIDe    protected voID onPause() {        super.onPause();        Log.v("test", "MainActivity onPause()方法");    }    @OverrIDe    protected voID onDestroy() {        super.onDestroy();        Log.v("test", "MainActivity onDestroy()方法");    }}

Acyicity_child.java

public class Activity_child extends AppCompatActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.child);        Log.v("test", "Activity_child 调用onCreate()");    }    public voID childClick(VIEw vIEw) {        Log.v("test", "Activity_child Click会调用startActivityForResult()");        Intent intent = new Intent(this, MainActivity.class);        intent.putExtra("extra_data", "Activity_child获取的位置模拟数据");        setResult(RSC, intent);        finish(); // 执行后会关闭此 Activity ,并且走如下流程: onPause(),onStop(),onDestory()    }    @OverrIDe    protected voID onStart() {        super.onStart();        Log.v("test", "Activity_child  onStart()方法 ");    }    @OverrIDe    protected voID onResume() {        super.onResume();        Log.v("test", "Activity_child  onResume()方法 ");    }    @OverrIDe    protected voID onRestart() {        super.onRestart();        Log.v("test", "Activity_child onRestart()方 法");    }    @OverrIDe    protected voID onStop() {        super.onStop();        Log.v("test", "Activity_child onStop()方法");    }    @OverrIDe    protected voID onPause() {        super.onPause();        Log.v("test", "Activity_child onPause()方法");    }    @OverrIDe    protected voID onDestroy() {        super.onDestroy();        Log.v("test", "Activity_child onDestroy()方法 ");    }}
结果

注意

1.当把Activity_child.java中childClick()的finish()换成startActivityForResult(intent, 1),那么数据传递失败,因为finish()是用来最后传递数据并且用来摧毁当前Activity的,而startActivityForResult()则用于接收数据,启动Activity。


2.当把MainActivity.java中onActivityResult()的判断语句的requestCode值改变,依旧不能传输数据,因为请求码不同,请求码需要相同才能传输。


3.当把MainActivity.java中onActivityResult()的判断语句的resultCode值改变,依旧不能传输数据,因为主页面接收的结果码和子页面返回的结果码不一致,两者必须一致才能传输。


4.当把MainActivity.java中onActivityResult()的getStringExtra()的参数值改变,只能取到空数据,因为主页面取的键和子页面传的键不一样不一致,主页面取不到对应的值,两者必须一致才能传输到正确的值。

总结

以上是内存溢出为你收集整理的Android之数据传递全部内容,希望文章能够帮你解决Android之数据传递所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存