(一)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.xml.
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.xml.
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()函数获取数据
4.传递引用
4.1使用序列化的方式传递引用类
自定义类:
public class User implements Serializable {        //使用java提供的序列化的方式   
private String name;
private int age; public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public int getAge() {
return age;
}
public User(String name,int age)
{
this.name=name;
this.age=age;
}
}
主Activity:
public class MainActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class); intent.putExtra("user", (Serializable) new User("henryQL",24)); //添加自定义的引用类的对象
startActivity(intent);
}
});
} }
被调用的Activity:
public class BActivity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b); Intent intent=getIntent();
User user= (User) intent.getSerializableExtra("user"); //获取序列化的数据 TextView tv= (TextView) findViewById(R.id.tv);
tv.setText(String.format("nmae=%s,age=%d",user.getName(),user.getAge()));
}
}
4.2 使用Android提供的Parcelable接口
public class User implements Parcelable {
private String name;
private int age; public void setName(String name) {
this.name = name;
} public void setAge(int age) {
this.age = age;
} public String getName() {
return name;
} public int getAge() {
return age;
}
public User(String name,int age)
{
this.name=name;
this.age=age;
} @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(getName());
parcel.writeInt(getAge());
} public static final Parcelable.Creator<User> CREATOR=new Parcelable.Creator<User>() //必须命名为CREATOR
{ @Override
public User createFromParcel(Parcel parcel) {
return new User(parcel.readString(),parcel.readInt());
} @Override
public User[] newArray(int i) {
return new User[i];
}
};
}
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("user",new User("Qianlong",24)); //startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
}
}
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();
User user=intent.getParcelableExtra("user"); //获取Parcelable对象 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")));
tv.setText(String.format("nmae=%s,age=%d",user.getName(),user.getAge()));
}
}
5.Activity传递返回参数
若想打开一个新的Activity的时候能够获取其返回的参数,必须使用startActivityForResult,而不能是之前的startActivity函数了;
此外必须在该类中重写函数onActivityResult,在该函数中用来响应返回的数据。在新的Activity中使用setRequest来发送要返回的数据,并使用finish函数来结束当前的Activity。
public class MainActivity extends Activity {

    private TextView rtnTextView;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); rtnTextView= (TextView) findViewById(R.id.rtnTextView); findViewById(R.id.newActivityBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//发送数据,并且能够获取另一activity的返回数据
Intent intent=new Intent(MainActivity.this,MainActivity2Activity.class);
intent.putExtra("name","henry");
intent.putExtra("age",24);
startActivityForResult(intent,1); //第二个参数是请求码,设置请求码和要发送的intent数据
}
}); } //响应MainActivity2Activity的返回数据
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
rtnTextView.setText(data.getStringExtra("data")); //获取返回的数据
}
}
public class MainActivity2Activity extends Activity {

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2); TextView tv=(TextView)findViewById(R.id.displayTextView);
Intent intent=getIntent();
tv.setText(String.format("name=%s,age=%d",intent.getStringExtra("name"),intent.getIntExtra("age",0))); //传递返回值
final EditText editText=(EditText)findViewById(R.id.editText);
findViewById(R.id.returnBtn).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1=new Intent();
intent1.putExtra("data",editText.getText().toString());
setResult(1,intent1); //设置返回码,和要返回的Intent
finish();
}
});
}
}
 
上一篇:Windows IOT


下一篇:android项目中各个文件的介绍