面试阿里P7:利用startActivityForResult如何返回数据到前一个Activity?(附源码+解析)

一线互联网大厂Java核心面试题库

image

正逢面试跳槽季,给大家整理了大厂问到的一些面试真题,由于文章长度限制,只给大家展示了部分题目,更多Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等已整理上传,感兴趣的朋友可以看看支持一波!

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:layout_gravity=“center”>

<TextView

android:id=“@+id/textView1”

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

android:text=“计算标准体重”

android:paddingTop=“20dp”

android:paddingLeft=“20dp”

android:textSize=“30sp”/>

<TextView

android:text=“性别:”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content” android:id=“@+id/textView3”

android:layout_alignStart=“@id/textView1” android:layout_marginTop=“38dp”

android:layout_below=“@id/textView1” android:layout_marginStart=“46dp”/>

<TextView

android:text=“身高:”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content” android:id=“@+id/textView4”

android:layout_alignStart=“@id/textView1” android:layout_marginStart=“46dp”

android:layout_below=“@id/textView3” android:layout_marginTop=“29dp”/>

<EditText android:layout_width=“wrap_content” android:layout_height=“wrap_content”

android:id=“@+id/editText”

android:layout_toEndOf=“@id/textView4”

android:layout_marginStart=“36dp”

android:autofillHints=“@string/app_name”

android:hint=“0”

android:layout_alignBaseline=“@id/textView4”/>

<TextView android:layout_width=“wrap_content” android:layout_height=“wrap_content”

android:text=“厘米”

android:layout_alignBaseline=“@id/editText”

android:layout_toRightOf=“@id/editText”

android:layout_marginStart=“10dp” />

<RadioButton

android:layout_below=“@id/textView1”

android:id=“@+id/radioButtonMale”

android:text=“男”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_alignStart=“@id/textView1” android:layout_marginTop=“30dp”

android:layout_marginStart=“113dp”/>

<RadioButton

android:id=“@+id/radioButtonFemale”

android:text=“女”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_below=“@id/textView1”

android:layout_toEndOf=“@id/radioButtonMale”

android:layout_marginLeft=“15dp” android:layout_marginTop=“30dp” android:layout_marginStart=“49dp”/>

<Button

android:text=“计算”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:id=“@+id/buttonGoToLayout2”

android:layout_marginTop=“90dp”

android:layout_below=“@id/radioButtonMale”

android:layout_alignStart=“@id/textView1” android:layout_marginStart=“92dp”/>

复制代码

3、Activity B的实现:

public class SecondActivity extends Activity {

private Intent mIntent;

private Bundle mBundle;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.second_layout);

mIntent = getIntent();

mBundle = mIntent.getExtras();

// 记得判空

if (mBundle == null) {

return;

}

// 获取Bundle中的数据

double height = mBundle.getDouble(“height”);

String gender = mBundle.getString(“gender”);

// 判断性别

String genderText = “”;

if (gender.equals(“M”)) {

genderText = “男性”;

} else {

genderText = “女性”;

}

// 获取标准体重

String weight = getWeight(gender, height);

// 设置需要显示的文字内容

TextView textView = findViewById(R.id.textView2);

textView.setText(“你是一位” + genderText + “\n你的身高是” + height + “厘米\n你的标准体重是” + weight + “公斤”);

Button button = findViewById(R.id.buttonGoBack);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

// 设置结果,并关闭页面

setResult(RESULT_OK, mIntent);

finish();

}

});

}

// 四舍五入格式化

private String format(double num) {

NumberFormat formatter = new DecimalFormat(“0.00”);

return formatter.format(num);

}

// 计算标准体重的方法

private String getWeight(String gender, double height) {

String weight = “”;

if (gender.equals(“M”)) {

weight = format((height - 80) * 0.7);

} else {

weight = format((height - 70) * 0.6);

}

return weight;

}

}

复制代码

4、Activity B的布局:

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<TextView

android:text=“This is the second layout”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:id=“@+id/textView2”

android:paddingTop=“30dp”

android:paddingStart=“50dp”/>

<Button android:layout_width=“wrap_content” android:layout_height=“wrap_content”

android:id=“@+id/buttonGoBack”

android:text=“回到上一页”

android:layout_alignStart=“@id/textView2”

android:layout_below=“@id/textView2”

android:layout_marginTop=“54dp” android:layout_marginStart=“52dp”/>

复制代码

不过这里有3个地方需要注意:

  1. startActivityForResult的第二个参数requestCode传的是0,那么我们分别看下传递的值小于0和大于0是什么结果:

(1)传一个小于0的值,比如-1:等同于调用 startActivity,onActivityResult不会被调用

(2)传一个大于0的值,比如1:效果等同于传0,onActivityResult的第一个参数正是我们通过startActivityForResult传递的requestCode

  1. onActivityResult的第二个参数resultCode:它是第二个activity通过setResult返回的,常用的取值有2个:RESULT_CANCELED、RESULT_OK

(1)RESULT_CANCELED:Activity B拉起失败,比如crash

(2)RESULT_OK:Activity B操作成功后的返回值

还有一个不太常用的取值:RESULT_FIRST_USER,Android源码对这个取值的定义是“user-defined activity results”(用户自定义的),我在源码中全局搜索了下,用的地方不多,挑了一两个使用的地方:

2.1 PackageInstaller下面的InstallFailed.java(安装apk失败的相关页面)

总结

其他的内容都可以按照路线图里面整理出来的知识点逐一去熟悉,学习,消化,不建议你去看书学习,最好是多看一些视频,把不懂地方反复看,学习了一节视频内容第二天一定要去复习,并总结成思维导图,形成树状知识网络结构,方便日后复习。

这里还有一份很不错的《Java基础核心总结笔记》,特意跟大家分享出来

目录:

部分内容截图:

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

总结笔记》,特意跟大家分享出来

目录:

[外链图片转存中…(img-7qrZQaN9-1715720161431)]

部分内容截图:

[外链图片转存中…(img-TscOHp7z-1715720161431)]

[外链图片转存中…(img-YfZvY7Zk-1715720161432)]

本文已被CODING开源项目:【一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码】收录

需要这份系统化的资料的朋友,可以点击这里获取

  • 26
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值