About activity This article summarizes the learning of the jump between pages, two ways, as well as passing parameters and receive return values.
About jump to the next page, two activity between the intent to communicate, two ways:
startActivity();startActivityForResult();
First, open the next page
Intent intent=new Intent(this, NewActivity.class); // 下面是另外3种设置激活组件的方法 //intent.setClass(this, NewActivity.class); //intent.setClassName(this, "com.example.mulactivity.NewActivity"); //intent.setComponent(new ComponentName(this, NewActivity.class)); startActivity(intent);
2. Pass parameters to the next page
Method of delivery:
publicvoidOpenNewActivity(View v){ Intent intent=new Intent(this, NewActivity.class); //第一种传值方式 //intent.putExtra("name", "atm"); //intent.putExtra("age", 10); //第二种传值方式 Bundle bundle=new Bundle(); bundle.putString("name""atm"); bundle.putInt("age",10); intent.putExtras(bundle); startActivity(intent);
The next page receives the method:
Intent intent=getintent ();The first method of receiving data//string name=intent. Getstringextra("Name");Integer age=intent. Getintextra("Age",0);The second method of accepting data//bundle Bundle = Intent. Getextras();String name = Bundle. getString("Name");int age = Bundle. GetInt("Age");TextView. SetText(name+"The Age is:"+age);
3. Get the value returned when the next page closes
Overriding the Onactivityresult () method
Public void opennewactivity(View v) {Intent intent=NewIntent ( This, Newactivity.class); Startactivityforresult (Intent, the); }@Override protected void Onactivityresult(intRequestcode,intResultCode, Intent data) {//TODO auto-generated method stubString res = Data.getstringextra ("res"); Toast.maketext ( This, res, Toast.length_long). Show ();//super.onactivityresult (Requestcode, ResultCode, data);}
Return a value to the top-level page when the page is closed:
publicvoidCloseActivity(View v){ new Intent(); intent.putExtra("res""这是我返回回来的数据"); setResult(20, intent); this.finish();//关闭当前的activity }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android Common components (one of activity learning)