大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
当一个OneActivity跳转到TwoActivity时,TwoActivity关闭时要把数据传回到OneActivity,这个时候就得用到StartActivityForResult。
使用这个方法特别要注意的是:一个Activity中可能包含多个按钮,并调用多个startActivityForResult方法来打开不同的Activity处理业务,当这些新Activity关闭后,系统会调用前面的Activity的onActivityResult方法。为了知道该方法是由哪个请求结果所触发的,可利用requestCode标识,为了知道返回的数据来自于那个新的Activity,可利用resultCode标识。
接下来我们通过一个栗子进行讲解,效果图如下(1跳转到2把值传过去显示出来,2跳转回1把值传回来显示出来):
由于上传图片受到限制,只能展现出两个界面的跳转。
1、布局文件就贴两个出来:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/bt_onclick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip_TO_Two" />
</LinearLayout>
activity_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".TwoActivity">
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<Button
android:id="@+id/bt_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip_TO_One" />
<Button
android:id="@+id/bt_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip_To_Three" />
</LinearLayout>
activity_three.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".ThreeActivity">
<TextView
android:id="@+id/tv_three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<Button
android:id="@+id/bt_threeOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Skip_To_Two" />
</LinearLayout>
2、java类:MainActivity.class
public class MainActivity extends AppCompatActivity {
private Button mBtn;
private TextView mText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mBtn = findViewById(R.id.bt_onclick);
mText = findViewById(R.id.tv_text);
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, TwoActivity.class);
Bundle bundle = new Bundle();
//用bundle传个key为oneContent,值为hello twoActivity到第二个界面
bundle.putString("oneContent", "OneActivty->TwoActivity");
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 1) {//接收后面一个界面结果码为1的界面
Bundle bundle = data.getExtras();//注意这里是data,就是这个方法里面的Intent对象,这才是后面界面传回来的intent。
String name = bundle.getString("twoContent");
mText.setText(name);
}
}
}
TwoActivity.class
public class TwoActivity extends AppCompatActivity {
private TextView mContent;
private Button mSkip, mSkipToThree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
mContent = findViewById(R.id.tv_text);
mSkip = findViewById(R.id.bt_two);
mSkipToThree = findViewById(R.id.bt_three);
Intent intent = getIntent();//获取前一个activity的传值
Bundle bundle = intent.getExtras();
String name = bundle.getString("oneContent");//获取前一个界面key为oneContent的值
mContent.setText(name);
mSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent(TwoActivity.this, MainActivity.class);
Bundle bundle1 = new Bundle();
bundle1.putString("twoContent", "TwoActivity->OneActivity");
intent1.putExtras(bundle1);
//把resultcode传回到前一个界面,并把intent1携带的值传回前一个界面
setResult(1, intent1);
finish();//记得要finish
}
});
mSkipToThree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(TwoActivity.this, ThreeActivity.class);
Bundle bundle2 = new Bundle();
bundle2.putString("threeContent", "TwoActivity->ThreeActivity");
intent2.putExtras(bundle2);
startActivityForResult(intent2, 3);
Log.i("start", "start");
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 4) {
Bundle bundle = data.getExtras();
String name = bundle.getString("ThreeContent");
mContent.setText(name);
}
}
}
ThreeActivity.class
public class ThreeActivity extends AppCompatActivity {
private TextView mContent;
private Button mSkip;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_three);
mContent = findViewById(R.id.tv_three);
mSkip = findViewById(R.id.bt_threeOther);
Intent intent =getIntent(); //获取前一个activity的传值
Bundle bundle =intent.getExtras();
Log.i("bundle","bundle="+bundle);
String name = bundle.getString("threeContent");
Log.i("name", "name="+name);
mContent.setText(name);
mSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent1 = new Intent();
Bundle bundle1 = new Bundle();
bundle1.putString("ThreeContent", "ThreeActivity->TwoActivity");
intent1.putExtras(bundle1);
setResult(4, intent1);
finish();//记得要finish
}
});
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/157730.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...