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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM