Transferring object data from one activity to another activity

前端 未结 5 1283
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-07 08:30

I am having a class EmployeeInfo as the following:

 public class EmployeeInfo {
        private int id; // Employee ID
        private String name; // Employ         


        
5条回答
  •  庸人自扰
    2021-01-07 08:42

    Well there is another way to transfer an object.We can use application to transfer object and this is way is far better way in my opinion.

    First of all create your custom application in your main package.

    public class TestApplication extends Application {
        private Object transferObj;
    
        @Override
        public void onCreate() {
            super.onCreate();
            // ACRA.init(this);
        }
    
        public Object getTransferObj() {
            return transferObj;
        }
    
        public void setTransferObj(Object transferObj) {
            this.transferObj = transferObj;
        }
    
    }
    

    Now use setTransfer and get transfer methods to move abjects from one activity to other like:

    To Transfer:

    ((TestApplication) activity.getApplication()).setTransferObj(Yous object);
    

    ToRecieve:

    Object obj=((TestApplication) activity.getApplication()).getTransferObj();
    

    NOTE Always remember to make entry of this application in manifest application tag:

    
    
    

提交回复
热议问题