How can I pass an object of a custom type from one Activity to another using the putExtra()
method of the class Intent?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
I know this is late but it is very simple.All you have do is let your class implement Serializable like
then you can pass to an intent like
To get it you simpley call
in your class model (Object) implement Serializable, for Example:
and your first Activity
and your second Activity (NewActivity)
good luck!!
Another way to do this is to use the
Application
object (android.app.Application). You define this in youAndroidManifest.xml
file as:You can then call this from any activity and save the object to the
Application
class.In the FirstActivity:
In the SecondActivity, do :
This is handy if you have objects that have application level scope i.e. they have to be used throughout the application. The
Parcelable
method is still better if you want explicit control over the object scope or if the scope is limited.This avoid the use of
Intents
altogether, though. I don't know if they suits you. Another way I used this is to haveint
identifiers of objects send through intents and retrieve objects that I have in Maps in theApplication
object.Using google's Gson library you can pass object to another activities.Actually we will convert object in the form of json string and after passing to other activity we will again re-convert to object like this
Consider a bean class like this
We need to pass object of Example class
For reading we need to do the reverse operation in NextActivity
Add this dependancy in gradle
In your first Activity:
And in your second one:
Don't forget to make your custom object Serializable: