Android 多个Activity 跳转及传参

mainActivity 打开 OtherActivity:

Intent intent = new Intent(getApplicationContext(), OtherActivity.class);            
startActivity(intent); 

mainActivity 给 OtherActivity 传参数: 

            Intent intent = new Intent(getApplicationContext(), OtherActivity.class);        
            //以下二个为OtherActivity传参数
            intent.putExtra("Name", "eboy");
            intent.putExtra("Age", 22);
            //也可以使用Bundle来传参数
            Bundle bundle = new Bundle();
            bundle.putString("Name1", "eboy1");
            bundle.putInt("Age1", 23);
            intent.putExtras(bundle);
            startActivity(intent);


 OtherActivity 接收来自 mainActivity 的参数:

        Intent intent = getIntent(); //用于激活它的意图对象
        
        String Name = intent.getStringExtra("Name");
        int Age = intent.getIntExtra("Age", 0);
        
        Bundle bundle = intent.getExtras();        
        
        String Name1 = bundle.getString("Name1");
        int Age1 = bundle.getInt("Age1");
        
        TextView textView = (TextView)this.findViewById(R.id.OtherTextView);
        textView.setText(Name + " : " + Age + "/" + Name1 + " : " + Age1);

如果mainActivity 需要 OtherActivity关闭时返回一些值,则可使用 startActivityForResult来打开OtherActivity,具体用法以后用到时再了解。

 /Files/jxgxy/MutilActivity.rar

原文地址:https://www.cnblogs.com/jxgxy/p/2617034.html