how to pass value data between classes/activity in Android?

后端 未结 6 2018
情书的邮戳
情书的邮戳 2021-01-03 13:12

For example i have activity1, activity2, activity3 and lastly valueAllActivity? how do I pass the data from activity1, activity2, activity3 to --> valueAllActivity?

6条回答
  •  温柔的废话
    2021-01-03 13:40

    You have some possibilities.

    One that I like more is using the application context...

    Create a new class like:

    public class DataApp extends Application{
    
        private int myInt;
        private MyCustomObject myObj;
    
    
        public int getMyInt()               {   return myInt;           }
        public void setMyInt(int i)     {   this.myInt = i; }
        public MyCustomObject getMyObj()                {   return myObj;       }
        public void setMyObj(MyCustomObject ob)     {   this.myObj = ob;}
    
    }
    

    Add this to you manifest:

    
    ...
    
        
    

    After, when you need to pass data you can do this in your activity:

    DataApp dataInfo = (DataApp)getApplicationContext();
    
    //set some data:
    
    dataInfo.setMyObj(/*put the object*/);
    

    In your other activity, you get you object like this:

    DataApp dataInfo = (DataApp)getApplicationContext();
    MyCustomObject obj = dataInfo.getMyObj();
    

    With this option, you can pass every object type you want.

提交回复
热议问题