編輯:關於android開發
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()));
}
}
Linux內核系列—操作系統開發之保護模式的優勢,linux內核在上一篇中我們雖然成功進入了保護模式,但是並沒有體驗到保護模式帶給我們的便利。其實在保護模式下尋址空間可以
【開源】二維碼簽到手機andriod版下載!,開源簽到andriod 最近,應很多人的要求,我的易簽到二維碼簽到andriod版現
[better practice系列]Android處理好activity正確情況下的生命周期和意外情況下的生命周期淺析 前言: Activity生命周期是每一個Andr
Android 自定義View高級特效,神奇的貝塞爾曲線 效果圖 效果圖中我們實現了一個簡單的隨手指滑動的二階貝塞爾曲線,還有一個復雜點的,穿越所有已知點的貝塞爾曲線。