ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

startActivityForResult跳转后回调数据

2022-02-02 11:33:58  阅读:156  来源: 互联网

标签:BActivity AActivity bundle intent Bundle 跳转 startActivityForResult 回调 new


从AActivity向BActivity跳转后,关闭BActivity并向AActivity回调一些数据:

建立AActivity.java文件:

 1 public class AActivity extends AppCompatActivity {
 2     private Button btnJump;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_aactivity);
 7         btnJump=findViewById(R.id.jump);
 8         btnJump.setOnClickListener(new View.OnClickListener() {
 9             @Override
10             public void onClick(View view) {
11                 //数据传递
12                 Intent intent=new Intent(AActivity.this,BActivity.class);
13                 Bundle bundle=new Bundle();
14                 bundle.putString("name","霉霉");
15                 bundle.putInt("age",30);
16                 intent.putExtras(bundle);
17                     //返回数据
18                 startActivityForResult(intent,0);//向BActivity跳转
19                 
20             }
21         });
22     }
23 
24     @Override
25     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
26         super.onActivityResult(requestCode, resultCode, data);
27         Toast.makeText(AActivity.this, data.getExtras().getString("title"), Toast.LENGTH_SHORT).show();
28     }//接收BActivity回调的数据
29 }

对应的activity_aactivity.xml文件为:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:padding="20dp">
 6 
 7     <Button
 8         android:id="@+id/jump"
 9         android:layout_width="match_parent"
10         android:layout_height="50dp"
11         android:text="jump"
12         android:textAllCaps="false"/>
13 </LinearLayout>

建立BActivity.java文件:

 1 public class BActivity extends AppCompatActivity {
 2     private TextView TVtitle2;
 3     private Button btnFinish;
 4     @Override
 5     protected void onCreate(Bundle savedInstanceState) {
 6         super.onCreate(savedInstanceState);
 7         setContentView(R.layout.activity_bactivity);
 8         TVtitle2=findViewById(R.id.tv_title2);
 9         btnFinish=findViewById(R.id.btn_finish);
10 
11         Bundle bundle=getIntent().getExtras();
12         String name=bundle.getString("name");
13         int age=bundle.getInt("age");//传递数据
14 
15         TVtitle2.setText(name+":"+age);
16 
17         btnFinish.setOnClickListener(new View.OnClickListener() {
18             @Override
19             public void onClick(View view) {
20                 Intent intent=new Intent();
21                 Bundle bundle1=new Bundle();
22                 bundle1.putString("title","I'm back!!");
23                 intent.putExtras(bundle1);
24                 setResult(Activity.RESULT_OK,intent);
25                 finish();//关闭BActivity文件并回调数据
26             }
27         });
28     }
29 }

对应的activity_bactivity.xml文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent"
 4     android:orientation="vertical"
 5     android:padding="20dp">
 6     <TextView
 7         android:id="@+id/tv_title2"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:textColor="@color/black"
11         android:textSize="20sp"
12         android:layout_margin="20dp" />
13 
14     <Button
15         android:id="@+id/btn_finish"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:text="点我回调数据"/>
19 </LinearLayout>

 

标签:BActivity,AActivity,bundle,intent,Bundle,跳转,startActivityForResult,回调,new
来源: https://www.cnblogs.com/konglong-cm/p/15860152.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有