你的位置:首页 > 操作系统

[操作系统](一)Activity参数传递

1.主Activity,用于启动另一个Activity()
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("onCreate"); findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);
intent.putExtra("data","Hello Henry"); //进行参数传递
//startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
} @Override
protected void onStart()
{
super.onStart();
System.out.println("onStrart");
}
@Override
protected void onRestart()
{
super.onRestart();
System.out.println("onRestart");
}
@Override
protected void onStop()
{
super.onStart();
System.out.println("onStop");
}
@Override
protected void onResume()
{
super.onResume();
System.out.println("onResume");
}
@Override
protected void onPause()
{
super.onPause();
System.out.println("onPause");
}
@Override
protected void onDestroy()
{
super.onDestroy();
System.out.println("onDestroy");
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest. int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
2.从Activity 被MainActivity启动
public class BActivity extends Activity {  @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
System.out.println("B onCreate"); Intent intent=getIntent(); //获取MainActivity传递过来的Intent TextView tv= (TextView) findViewById(R.id.tv);
tv.setText(intent.getStringExtra("data")); //调用Intent的getStringExtra("data")方法取出数据
  }  @Override
protected void onStart()
{
super.onStart();
System.out.println("B onStrart");
}
@Override
protected void onRestart()
{
super.onRestart();
System.out.println("B onRestart");
}
@Override
protected void onStop()
{
super.onStart();
System.out.println("B onStop");
}
@Override
protected void onResume()
{
super.onResume();
System.out.println("B onResume");
}
@Override
protected void onPause()
{
super.onPause();
System.out.println("B onPause");
}
@Override
protected void onDestroy()
{
super.onDestroy();
System.out.println("B onDestroy");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_b, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest. int id = item.getItemId(); //noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
} return super.onOptionsItemSelected(item);
}
}
3.传递数据包Bundle
3.1 MainActivity中的findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
    @Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);
//intent.putExtra("data","Hello Henry"); //传递数据包
Bundle bundle=new Bundle();
bundle.putString("name","henry"); //往数据包中添加数据
bundle.putInt("age",24);
intent.putExtras(bundle); //将数据包添加到Intent中 //startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
}
3.2 BActivity中的
Intent intent=getIntent();
Bundle bundle=intent.getExtras(); //接收数据包TextView tv= (TextView) findViewById(R.id.tv);
//tv.setText(intent.getStringExtra("data"));
tv.setText(String.format("name=%s,age=%d",bundle.getString("name"),(int)bundle.get("age"))); //调用Bundle的getXXX()函数获取数据