其他分享
首页 > 其他分享> > Activity通过bundle传递数据

Activity通过bundle传递数据

作者:互联网

从AActivity.java向BActivity.java传递数据:

建立AActivity.java文件建立bundle:

 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                 startActivity(intent);
18 
19             }
20         });
21     }
22 }

在BActivity.java文件中接收bundle:

 1 public class BActivity extends AppCompatActivity {
 2     private TextView TVtitle2;
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_bactivity);
 7         TVtitle2=findViewById(R.id.tv_title2);
 8 
 9         Bundle bundle=getIntent().getExtras();
10         String name=bundle.getString("name");
11         int age=bundle.getInt("age");//传递数据
12 
13         TVtitle2.setText(name+":"+age);
14     }
15 }

 

标签:BActivity,java,savedInstanceState,Bundle,bundle,Activity,传递数据,onCreate
来源: https://www.cnblogs.com/konglong-cm/p/15860119.html