其他分享
首页 > 其他分享> > startActivityForResult跳转后回调数据

startActivityForResult跳转后回调数据

作者:互联网

从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