Andriod--学习-Activity的启动方式--Activ

2017-06-28  本文已影响27人  charleswang

实现简单的Activity之间的跳转

button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FActivity.this,SActivity.class);
                startActivity(intent);
            }
        });

传递数据到另一个Activity中

1.传递基本类型和数组

button = (Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
            @Override
        public void onClick(View v) {
             Intent intent = new Intent(this,OtherActivity.class);
             intent.putExtra("name", "二狗子");//键值对
            intent.putExtra("age", 18);
            startActivity(intent);
            }
        });

String name = i.getStringExtra("name");
int age = i.getIntExtra("age",-1);//若为空时为-1

2.传递对象

Intent intent = new
Bundle bundle = new Bundle();
bundle.putString("name", "二狗子");
bundle.putInt("age", 18);
intent.putExtra("bundle", bundle);
startActivity(intent);

1.Serializable

public class Author implements Serializable{
    private int id;
 
    private String name;
 
    //...
}
public class Book implements Serializable{
    private String title;
    private Author author;
    //...
}
 Book book=new Book(); 
  book.setTitle("Java编程思想"); 
  Author author=new Author(); 
  author.setId(1); 
  author.setName("Bruce Eckel"); 
  book.setAuthor(author); 
  Intent intent=new Intent(this,SecondActivity.class); 
  intent.putExtra("book",book); 
  startActivity(intent);
 Book book= (Book) getIntent().getSerializableExtra("book");
 Log.d(TAG,"book title->"+book.getTitle());
 Log.d(TAG,"book author name->"+book.getAuthor().getName());

2.Parcelable

实现Parcelable接口需要实现两个方法

public class Author implements Parcelable{
    private int id;
 
    private String name;
 
    //setter & getter...
 
    @Override
    public int describeContents() {
 
        return 0;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        //该方法将类的数据写入外部提供的Parcel中.即打包需要传递的数据到Parcel容器保存,
        // 以便从parcel容器获取数据
        dest.writeString(name);
        dest.writeInt(id);
 
    }
    public static final Creator<Author> CREATOR=new Creator<Author>() {
        @Override
        public Author createFromParcel(Parcel source) {
            //从Parcel容器中读取传递数据值,封装成Parcelable对象返回逻辑层。
            Author author=new Author();
            author.setName(source.readString());
            author.setId(source.readInt());
            return author;
        }
 
        @Override
        public Author[] newArray(int size) {
            //创建一个类型为T,长度为size的数组,仅一句话(return new T[size])即可。方法是供外部类反序列化本类数组使用。
            return new Author[size];
        }
    };
}

public class Book implements Parcelable{
    private String title;
    private Author author;
    //setter & getter...
 
    @Override
    public int describeContents() {
        return 0;
    }
 
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
        dest.writeParcelable(author,flags);
    }
    public static final Creator<Book> CREATOR=new Creator<Book>() {
        @Override
        public Book createFromParcel(Parcel source) {
            Book book=new Book();
            book.setTitle(source.readString());
            book.setAuthor(source.<Author>readParcelable(Author.class.getClassLoader()));
            return book;
        }
 
        @Override
        public Book[] newArray(int size) {
            return new Book[0];
        }
    };
}
Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book",book);
startActivity(intent);
Book book=getIntent().getParcelableExtra("book");
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());

使用StartActivityForResult(intent, requestCode)方法实现Activity间数据的回传

实现的步骤:

方法的详解:

下面实例说明:
在原Activity中

Intent intent = new Intent(this,OtherActivity.class);
//回传数据
startActivityForResult(intent, 101);//请求码 标示作用

重写==onActivityResult==方法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    //在这里判断是从哪个界面传递回的数据
        if(requestCode==101 && resultCode ==102){
            String name = data.getStringExtra("name");
            Toast.makeText(this,"name:"+name, 1).show();
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

在==OtherActivity==中:

Intent i = getIntent();
        
i.putExtra("name", "我是回传的数据");
setResult(102, i);//结果码  起一个标示作用
//结束当前activity
finish();
上一篇 下一篇

猜你喜欢

热点阅读