How to pass an object from one activity to another on Android

后端 未结 30 3987
遇见更好的自我
遇见更好的自我 2020-11-21 04:03

I am trying to work on sending an object of my customer class from one Activity and display it in another Activity.

The code for t

30条回答
  •  闹比i
    闹比i (楼主)
    2020-11-21 04:55

    It is impossible to serialize any type of object. For Example you can not serialize delegate methods or interfaces that carry code instead of data. So I have written a 'Box' class you can use to pass any type of data without Serialization.

    1- for putting Data to Intent Use:

    Intent I = new Intent(this, YourActivity.class);
    CustomClass Date = new CustomClass();
    Box.Add(I, "Name", Data);
    

    2- for retrieving Data from Intent:

    CustomClass Data = Box.Get(getIntent(), "Name");
    

    3- for removing data after use, add this method to your activity:

    @Override
    protected void onDestroy() {
        Box.Remove(getIntent());
        super.onDestroy();
    }
    

    4- and add this code to your project:

    package ir.namirasoft.Utility;
    
    import android.content.Intent;
    
    import java.util.HashMap;
    import java.util.Vector;
    
    public class Box {
        // Number
        private static int Number = 1;
    
        public static int NextNumber() {
            return Number++;
        }
    
        //
        private static String _Intent_Identifier = "_Intent_Identifier";
        private static HashMap> DeleteList = new HashMap>();
        private static HashMap> ObjectList = new HashMap>();
    
        public static int GetIntent_Identifier(Intent I) {
            int Intent_Identifier = I.getIntExtra(_Intent_Identifier, 0);
            if (Intent_Identifier == 0)
                I.putExtra(_Intent_Identifier, Intent_Identifier = NextNumber());
            return Intent_Identifier;
        }
    
        public static void Add(Intent I, String Name, Object O) {
            int Intent_Identifier = GetIntent_Identifier(I);
            synchronized (ObjectList) {
                if (!ObjectList.containsKey(Intent_Identifier))
                    ObjectList.put(Intent_Identifier, new HashMap());
                ObjectList.get(Intent_Identifier).put(Name, O);
            }
        }
    
        public static  T Get(Intent I, String Name) {
            int Intent_Identifier = GetIntent_Identifier(I);
            synchronized (DeleteList) {
                DeleteList.remove(Intent_Identifier);
            }
            return (T) ObjectList.get(Intent_Identifier).get(Name);
        }
    
        public static void Remove(final Intent I) {
            final int Intent_Identifier = GetIntent_Identifier(I);
            final int ThreadID = NextNumber();
            synchronized (DeleteList) {
                if (!DeleteList.containsKey(Intent_Identifier))
                    DeleteList.put(Intent_Identifier, new Vector());
                DeleteList.get(Intent_Identifier).add(ThreadID);
            }
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(60 * 1000);
                    } catch (InterruptedException e) {
                    }
                    synchronized (DeleteList) {
                        if (DeleteList.containsKey(Intent_Identifier))
                            if (DeleteList.get(Intent_Identifier).contains(ThreadID))
                                synchronized (ObjectList) {
                                    ObjectList.remove(Intent_Identifier);
                                }
                    }
                }
            }).start();
        }
    }
    

    ** Box class is Thread-Safe.

提交回复
热议问题