• Serializable方式实现数据传递


    FirstActivity.java

    package com.sdutacm.getcontextskillpractise;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class FirstActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Person person = new Person();
            person.setAge(18);
            person.setName("Tom");
            Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
            intent.putExtra("person_data",person);
            startActivity(intent);
        }
    }
    

    SecondActivity.java

    package com.sdutacm.getcontextskillpractise;
    
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    
    /**
     * Created by bummer on 2017/8/9.
     */
    
    public class SecondActivity extends AppCompatActivity{
       public static String TAG = "SecondActivity";
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Person person = (Person) getIntent().getSerializableExtra("person_data");
            Log.d(TAG,"person name is "+person.getName());
            Log.d(TAG,"person age is "+person.getAge());
    
        }
    }
    

    Person.java

    package com.sdutacm.getcontextskillpractise;
    
    import java.io.Serializable;
    
    /**
     * Created by bummer on 2017/8/9.
     */
    
    public class Person implements Serializable {
       private String name;
        private int age;
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getAge() {
            return age;
        }
    
        public void setAge(int age) {
            this.age = age;
        }
    }
    
  • 相关阅读:
    Shape详解
    C#装箱与拆箱
    C#值类型、引用类型的区别
    C#类型简述
    C# 关键字列表
    python图片转字符画
    软件测试面试题
    python关键字以及含义,用法
    JMeter的那些问题
    APP测试功能点
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/11781855.html
Copyright © 2020-2023  润新知