【Android】-- 向上个和下个Activity页面发送数据

 Bundle

在代码中发送消息包裹,调用意图对象的putExtras方法,即可存入消息包裹。

在代码中接收消息包裹,调用意图对象的getExtras方法,即可取出消息包裹。

例:

        //创建一个包裹
        Bundle bundle = new Bundle();
        bundle.putString("request_content",tv_send.getText().toString());
        intent.putExtras(bundle);

 

一、向下一个Activity发送数据

Intent使用Bundle对象存放待传递的数据信息。

Bundle对象操作各类型数据的读写,方法说明如下:

例:

发送页面的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="这是要发送的文字"/>

    <Button
        android:id="@+id/btn_send"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="发送文字"/>

</LinearLayout>

java类

public class ActSendActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_send;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_send);
        tv_send = findViewById(R.id.tv_send);
        findViewById(R.id.btn_send).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(this,ActReceiveActivity.class);
        //创建一个包裹
        Bundle bundle = new Bundle();
        bundle.putString("request_content",tv_send.getText().toString());
        intent.putExtras(bundle);
        startActivity(intent);
    }
}

 接收页面的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

</LinearLayout>

java类

public class ActReceiveActivity extends AppCompatActivity {

    private TextView v_receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_receive);
        v_receive = findViewById(R.id.tv_receive);
        //从上一个页面传来的意图中获取包裹
        Bundle bundle = getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求消息:\n内容为:%s",request_content);
        v_receive.setText(desc);
    }
}

 

 

 

二、向上一个Activity返回数据

处理下一个页面的应答数据,详细步骤说明如下:

  • 上一个页面打包好请求数据,调用startActivityForResult方法执行跳转动作。
  • 下一个页面接收并解析请求数据,进行相应处理
  • 下一个页面在返回上一个页面时,打包应答数据并调用setResult方法返回数据包裹。
  • 上一个页面重写方法onActivityResult,解析获得下一个页面的返回数据。

例:

请求页面xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <Button
        android:id="@+id/btn_request"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="传送请求数据"/>

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


</LinearLayout>

java类

public class ActResquestActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String mRequest = "i'm here";
    private ActivityResultLauncher<Intent> register;
    private TextView tv_response;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_resquest);
        TextView tv_request = findViewById(R.id.tv_request);
        tv_request.setText("待发送的信息:"+mRequest);
        
        tv_response = findViewById(R.id.tv_response);
                
        findViewById(R.id.btn_request).setOnClickListener(this);
        
        register = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),result -> {
            if(result != null){
                Intent intent = result.getData();
                if(intent != null && result.getResultCode() == Activity.RESULT_OK){
                    Bundle bundle = intent.getExtras();
                    String response_content = bundle.getString("response_content");
                    String desc = String.format("收到返回消息:%s",response_content);
                    tv_response.setText(desc);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        Intent intent =new Intent(this,ActResponseActivity.class);
        //创建一个包裹
        Bundle bundle = new Bundle();
        bundle.putString("request_content",mRequest);
        intent.putExtras(bundle);
        register.launch(intent);
    }
}

应答页面xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

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

    <Button
        android:id="@+id/btn_response"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="返回应答数据"/>

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

</LinearLayout>

java类

public class ActResponseActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String mresponse = "over";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_act_response);

        TextView tv_request = findViewById(R.id.tv_request);
        Bundle bundle = getIntent().getExtras();
        String request_content = bundle.getString("request_content");
        String desc = String.format("收到请求:%s",request_content);
        tv_request.setText(desc);
        findViewById(R.id.btn_response).setOnClickListener(this);
        TextView tv_response = findViewById(R.id.tv_response);
        tv_response.setText("待返回消息:"+mresponse);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        bundle.putString("response_content",mresponse);
        intent.putExtras(bundle);
        //携带意图返回上一页面,RESULT_OK表示处理成功
        setResult(Activity.RESULT_OK,intent);
        //结束页面
        finish();
    }
}

 

 


  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Android 中,我们可以使用 `preLoad()` 方法来预加载下一个 `Activity` 页面。这个方法会在当前 `Activity` 中异步地加载下一个 `Activity` 页面的布局和资源,以提高用户体验。 以下是一个预加载下一个 `Activity` 页面的示例代码: ```java private void preLoadNextActivity() { Intent intent = new Intent(this, NextActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); overridePendingTransition(0, 0); new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(5000); // 模拟加载时间 } catch (InterruptedException e) { e.printStackTrace(); } // 加载完成后,销毁预加载的 Activity finishActivity(NextActivity.class.hashCode()); } }).start(); } ``` 上面的代码中,我们首先创建了一个 `Intent` 对象,用来启动下一个 `Activity` 页面。然后,我们使用 `startActivity()` 方法启动这个 `Activity`,并设置了 `FLAG_ACTIVITY_NO_ANIMATION` 标志位,以避免动画效果。接着,我们使用 `overridePendingTransition()` 方法来去掉当前 `Activity` 的退场动画。这样做可以提高用户体验,使用户感觉当前 `Activity` 顺畅地过渡到了下一个 `Activity`。 接下来,我们在一个新线程中模拟加载时间,等待 5 秒钟。在等待时间结束后,我们使用 `finishActivity()` 方法销毁预加载的 `Activity`,以释放资源。 最后,我们可以在当前 `Activity` 中调用 `preLoadNextActivity()` 方法来预加载下一个 `Activity` 页面。这样,在用户点击跳转按钮时,下一个 `Activity` 页面就能够快速地加载出来,提高用户体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

四月天行健

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值