How to send an object from one Android Activity to another using Intents?

后端 未结 30 4522
-上瘾入骨i
-上瘾入骨i 2020-11-21 04:47

How can I pass an object of a custom type from one Activity to another using the putExtra() method of the class Intent?

30条回答
  •  没有蜡笔的小新
    2020-11-21 05:22

    Create Android Application

    File >> New >> Android Application

    Enter Project name: android-pass-object-to-activity

    Pakcage: com.hmkcode.android

    Keep other defualt selections, go Next till you reach Finish

    Before start creating the App we need to create POJO class “Person” which we will use to send object from one activity to another. Notice that the class is implementing Serializable interface.

    Person.java

    package com.hmkcode.android;
    import java.io.Serializable;
    
    public class Person implements Serializable{
    
        private static final long serialVersionUID = 1L;
    
        private String name;
        private int age;
    
            // getters & setters....
    
        @Override
        public String toString() {
            return "Person [name=" + name + ", age=" + age + "]";
        }   
    }
    

    Two Layouts for Two Activities

    activity_main.xml

    
    
    
        
    
        
            
        
    
    
    
    
    
    
    
    

    activity_another.xml

    
    
    
    
    
    

    Two Activity Classes

    1)ActivityMain.java

    package com.hmkcode.android;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class MainActivity extends Activity implements OnClickListener {
    
    Button btnPassObject;
    EditText etName, etAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        btnPassObject = (Button) findViewById(R.id.btnPassObject);
        etName = (EditText) findViewById(R.id.etName);
        etAge = (EditText) findViewById(R.id.etAge);
    
        btnPassObject.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View view) {
    
        // 1. create an intent pass class name or intnet action name 
        Intent intent = new Intent("com.hmkcode.android.ANOTHER_ACTIVITY");
    
        // 2. create person object
        Person person = new Person();
        person.setName(etName.getText().toString());
        person.setAge(Integer.parseInt(etAge.getText().toString()));
    
        // 3. put person in intent data
        intent.putExtra("person", person);
    
        // 4. start the activity
        startActivity(intent);
    }
    
    }
    

    2)AnotherActivity.java

    package com.hmkcode.android;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class AnotherActivity extends Activity {
    
    TextView tvPerson;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_another);
    
        // 1. get passed intent 
        Intent intent = getIntent();
    
        // 2. get person object from intent
        Person person = (Person) intent.getSerializableExtra("person");
    
        // 3. get reference to person textView 
        tvPerson = (TextView) findViewById(R.id.tvPerson);
    
        // 4. display name & age on textView 
        tvPerson.setText(person.toString());
    
    }
    }
    

提交回复
热议问题