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

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

提交回复
热议问题