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

后端 未结 30 3683
-上瘾入骨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:07

    Easiest and java way of doing is : implement serializable in your pojo/model class

    Recommended for Android for performance view: make model parcelable

    0 讨论(0)
  • 2020-11-21 05:08

    You can send serializable object through intent

    // send where details is object
    ClassName details = new ClassName();
    Intent i = new Intent(context, EditActivity.class);
    i.putExtra("Editing", details);
    startActivity(i);
    
    
    //receive
    ClassName model = (ClassName) getIntent().getSerializableExtra("Editing");
    
    And 
    
    Class ClassName implements Serializable {
    } 
    
    0 讨论(0)
  • 2020-11-21 05:08

    Your class should implements Serializable or Parcelable.

    public class MY_CLASS implements Serializable
    

    Once done you can send an object on putExtra

    intent.putExtra("KEY", MY_CLASS_instance);
    
    startActivity(intent);
    

    To get extras you only have to do

    Intent intent = getIntent();
    MY_CLASS class = (MY_CLASS) intent.getExtras().getSerializable("KEY");
    

    If your class implements Parcelable use next

    MY_CLASS class = (MY_CLASS) intent.getExtras().getParcelable("KEY");
    

    I hope it helps :D

    0 讨论(0)
  • 2020-11-21 05:08

    Start another activity from this activity pass parameters via Bundle Object

    Intent intent = new Intent(getBaseContext(), YourActivity.class);
    intent.putExtra("USER_NAME", "xyz@gmail.com");
    startActivity(intent);
    

    Retrieve on another activity (YourActivity)

    String s = getIntent().getStringExtra("USER_NAME");
    

    This is ok for simple kind data type. But if u want to pass complex data in between activity u need to serialize it first.

    Here we have Employee Model

    class Employee{
        private String empId;
        private int age;
        print Double salary;
    
        getters...
        setters...
    }
    

    You can use Gson lib provided by google to serialize the complex data like this

    String strEmp = new Gson().toJson(emp);
    Intent intent = new Intent(getBaseContext(), YourActivity.class);
    intent.putExtra("EMP", strEmp);
    startActivity(intent);
    
    Bundle bundle = getIntent().getExtras();
        String empStr = bundle.getString("EMP");
                Gson gson = new Gson();
                Type type = new TypeToken<Employee>() {
                }.getType();
                Employee selectedEmp = gson.fromJson(empStr, type);
    
    0 讨论(0)
  • 2020-11-21 05:10

    For situations where you know you will be passing data within an application, use "globals" (like static Classes)

    Here is what Dianne Hackborn (hackbod - a Google Android Software Engineer) had to say on the matter:

    For situations where you know the activities are running in the same process, you can just share data through globals. For example, you could have a global HashMap<String, WeakReference<MyInterpreterState>> and when you make a new MyInterpreterState come up with a unique name for it and put it in the hash map; to send that state to another activity, simply put the unique name into the hash map and when the second activity is started it can retrieve the MyInterpreterState from the hash map with the name it receives.

    0 讨论(0)
  • 2020-11-21 05:12

    If you have a singleton class (fx Service) acting as gateway to your model layer anyway, it can be solved by having a variable in that class with getters and setters for it.

    In Activity 1:

    Intent intent = new Intent(getApplicationContext(), Activity2.class);
    service.setSavedOrder(order);
    startActivity(intent);
    

    In Activity 2:

    private Service service;
    private Order order;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quality);
    
        service = Service.getInstance();
        order = service.getSavedOrder();
        service.setSavedOrder(null) //If you don't want to save it for the entire session of the app.
    }
    

    In Service:

    private static Service instance;
    
    private Service()
    {
        //Constructor content
    }
    
    public static Service getInstance()
    {
        if(instance == null)
        {
            instance = new Service();
        }
        return instance;
    }
    private Order savedOrder;
    
    public Order getSavedOrder()
    {
        return savedOrder;
    }
    
    public void setSavedOrder(Order order)
    {
        this.savedOrder = order;
    }
    

    This solution does not require any serialization or other "packaging" of the object in question. But it will only be beneficial if you are using this kind of architecture anyway.

    0 讨论(0)
提交回复
热议问题