安卓携带数据的页面跳转

在这里插入图片描述

MainActivity

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button1=findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                //设置已在清单文件中配置的动作名称
                intent.setAction("android.intent.action.ysdy" );  //隐式
                startActivityForResult(intent,2);
            }
        });
        Button button2=findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                //简单用法
                intent.setClass(MainActivity.this,ThirdActivity.class);  //组件显式调用
                /*//其它写法
                ComponentName componentName = new ComponentName(MainActivity.this,ThirdActivity.class);
                intent.setComponent(componentName);*/

                Bundle bundle=new Bundle(); //创建Bundle对象
                bundle.putString("name","张三");bundle.putInt("age",20);  //键值对数据存入Bundle对象
                intent.putExtra("data",bundle);  //通过意图对象携带捆绑数据
                intent.putExtra("data2","未捆绑的数据");
                startActivityForResult(intent,3);
            }
        });

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //判断从哪个Activity返回
        Toast.makeText(this, "从第"+requestCode+"个Activity返回。", Toast.LENGTH_LONG).show();
        if(resultCode==3){
            String string=data.getStringExtra("hello");  //获取返回数据(非捆绑)
            Toast.makeText(this, string, Toast.LENGTH_LONG).show();
        }
    }
}

SecondActivity

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);


        Button button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                Log.i("wutest", "第2个Activity准备销毁...: ");
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.i("wutest", "第2个Activity销毁了。 ");
        //当前Activity销毁后,返回父Activity。
        // 如果在主Activity中使用finish()方法,将退出应用,其作用如同按返回键。
    }
}

ThirdActivity

public class ThirdActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);

        String receiver="接收的数据如下:\n";
        //获取主调组件的意图对象
        Intent intent=getIntent();
        //按调用时捆绑的键名
        Bundle data = intent.getBundleExtra("data");
        receiver+="name:"+data.getString("name")+"\n";
        receiver+="age:"+data.getInt("age");
        //获取非捆绑数据
        receiver+="\n"+intent.getStringExtra("data2");
        Toast.makeText(this, receiver, Toast.LENGTH_LONG).show();

        //向主Activity发送数据(未使用Bundle)
        intent=new Intent();
        intent.putExtra("hello","How are you?");
        //设置结果码,以方便主调组件从特定组件里获取数据
        setResult(3,intent);  //第1参数是设置结果码
    }
}

activity_main.xml

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="107dp"
        android:layout_marginTop="63dp"
        android:text="调用第2个Activity"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="46dp"
        android:text="调用第3个Activity"
        app:layout_constraintStart_toStartOf="@+id/button1"
        app:layout_constraintTop_toBottomOf="@+id/button1" />

activity_second.xml

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="我是被调用的Activity."
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="124dp"
        android:layout_marginTop="148dp"
        android:text="主动销毁当前活动"
        app:layout_constraintStart_toStartOf="@+id/textView1"
        app:layout_constraintTop_toTopOf="@+id/textView1" />

activity_third.xml

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="第3个Activity"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

清单文件

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".ThirdActivity"></activity>
        <activity
            android:name=".SecondActivity"
            android:label="隐式调用SecondActivity">
            <intent-filter>
                <action android:name="android.intent.action.ysdy" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值