zoukankan      html  css  js  c++  java
  • android: Intent启动activity

    使用Intent启动activity:

    Intent intent = new Intent(MainActivity.this, OtherActivity.class);
    
    startActivity(intent);

    使用Intent启动其他app的activity:

    Intent intent = new Intent();
    intent.setClassName("com.example.project1","com.example.project1.MainActivity");
    startActivity(intent);
    com.example.project1是AndroidManifest.xml文件中的package值,
    com.example.project1.MainActivity是目标Activity的完整类名。


    Intent传值:
    intent.putExtra("key", value);

    Intent传类实例:
    User user=new User("cc","lulu");
    intent.putExtra("obj",user);
    //getIntent().getSerializableExtra("obj");
    或者:
    User user=new User("cc","lulu");
    Bundle bundle=new Bundle();
    bundle.putSerializable("obj", user);
    intent.putExtra("obj", bundle);

    User类必须实现Serializable接口。

    Parcelable方式:

    public class User2 implements Parcelable {
        private String name;
        private String password;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    
        public User2() {
        }
    
        public User2(String name, String password) {
            super();
            this.name = name;
            this.password = password;
        }
    
        @Override
        public String toString() {
            return "User [name=" + name + ", password=" + password + "]";
        }
    
        @Override
        public int describeContents() {
            return 0;
        }
    
        /**
         * 调用Parcel#writeXXX()方法把User2字段一一写出。类型要对应。
         */
        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(name);
            dest.writeString(password);
    
        }
    
        /**
         * 必须提供一个名为CREATOR的常量。
         */
        public static final Parcelable.Creator<User2> CREATOR = new Parcelable.Creator<User2>() {
    
            /**
             * 调用Parcel#readXxx()方法,读取顺序必须与写出顺序相同。
             */
            @Override
            public User2 createFromParcel(Parcel source) {
                User2 user2 = new User2();
                user2.name = source.readString();
                user2.password = source.readString();
                return user2;
            }
    
            /**
             * new出一个数组
             */
            @Override
            public User2[] newArray(int size) {
                return new User2[size];
            }
    
        };
    }

    存数据:

    Intent intent=new Intent(MainActivity.this,OtherActivity.class);
    User2 user2=new User2("cc","lubang");
    intent.putExtra("obj", user2);
    startActivity(intent);

    取数据:

    User2 user = intent.getParcelableExtra("obj");
    Log.i("OtherActivity", user.toString());

    Parcelable传递数据效率更高。

  • 相关阅读:
    SqlServer卸载实例
    java写的各种钟(收集)
    Codeforces 1003D Coins and Queries 【性质】
    Codeforces 997B Roman Digits【暴力】【枚举】
    洛谷 P2679 子串 【dp神题】【滚动数组】【2015 noip d2t2】
    复习图论
    Codeforces 1000D Yet Another Problem On a Subsequence 【dp】【组合数学】
    Codeforces 1000C Covered Points Count 【前缀和优化】
    Codeforces 999F Cards and Joy 【dp】【性质】
    Codeforces 999D Equalize the Remainders 【模拟】
  • 原文地址:https://www.cnblogs.com/mada0/p/4817327.html
Copyright © 2011-2022 走看看