Android Intent :-Use Intent to pass class objects from Activities [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to send an object from one Android Activity to another using Intents?
I want to pass a class object from an Activity to other when one Activity calls to the other. I am trying to use Intent.putExtra(name, value) to make that but then I donĀ“t have Intent.getExtra(name) method. There are a lot of methods like getStringExtra(name), getDataExtra(name) and so on but what I put in the value of putExtra is a class object that have a lot of different values as String, Data and int. How can I make to pass that class?

You can use Serializable or Parcelable
If you use Serializable you only have to implement it by writing implements Serializable
If you use Parcelable you have to fill in the used methodes. See: http://developer.android.com/reference/android/os/Parcelable.html
Read about the differences here: http://www.mooproductions.org/node/6
Conclusion: Parcelable is really alot faster but takes more time to implement.

In order to pass your custom objects through Intent, you need to make sure that your class implements Parcelable.
For more info, read this tutorial.

Related

Pass data between activities Android [duplicate]

This question already has answers here:
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I want to know if there is a way to pass a class object, which can't implement serializable interface between 2 android activities.
I know that to pass data using intent you have to implement serializable interface, but unfortunately I can't do this, any idea?
public class ContactMongo implements Serializable{
private DB collection;
public ContactMongo()
{ }
}
DB doesn't implement serializable, so I can't perform intent.putExtra();
You probably want to use a service, but you haven't told us anything about what you're trying to pass, so I guess you want to use ugly hacks.
There is such an ugly hack available, but it'll quickly get out of control (especially if you cannot even search on this topic by yourself) - you can extend your Application class and use its methods to pass data between activities. Generally, it should be avoided and other ways to pass data and let activities communicate should be used (like extracting the data from the object you want to pass).
Hope this helps you, comment if you have any questions (I'll update my answer with better solution if you give more information on what you're trying to do)
You need to send an intent with whatever you want to pass.
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
Object object;
intent.putExtra("nameforyourobject", object);
startActivity(intent);
https://developer.android.com/training/basics/firstapp/starting-activity.html

Android: Passing object from Activity to Service

I follow this link Pass data from activity to service for understanding the passage of data from Activity to Service Android. The problem is that I need to pass a complex object to Service. How can I achieve this task?
You can make your complex object implement Serializable or Parcelable and then you can pass it to Service using putExtra(String name, Serializable value) or putExtra(String name, Parcelable value)
Here you can find a tutorial for implementing Parcelable and here Serializable one.
If you aren't familiar with an any of them I would suggest you to use `Serializable' since it's more simple and pretty straightforward
Passing in custom objects is a little more complicated. You could just mark the class as Serializable
and let Java take care of this. However, on the android, there is a serious performance hit that comes with using Serializable. The solution is to use Parcelable.
Refer the examples:
http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/
http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html

How to Keep XMPP connection alive throughout application? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Android: How do i pass an object from one activity to another?
I have one class XmppClient();
instantiating in MainActivity like this
XmmppClient client = new XmmppClient ();
and have to use that in another activity how can i keep it alive for my application..
You can passing Custom Obejct to Between two Activities:
1. Using Application Class
2. By implementing Parcelable interface in your MyView class
3. By implementing Serializable interface in your MyView class
Yes, its possible only if your custom object class implements Parcelable interface.
One option could be letting your custom class implement Serializable interface and then you can pass object instances in intent extra using putExtra(Serializable..) variant of the Intent#putExtra() method.
PSEUDO code:
//to pass :
intent.putExtra("MyClass", obj);
// to retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");

how to save data in next activity

hi to all
I what to know how to save data in next activity
Use Intent to pass data from one Activity to another Activity.
But these Intents may carry native data types like String, int, float, double etc.
If you want your own Object to be passed, you would have to implement Serializable interface.
An odd way is to use a static Object in a class which is globally accessible.
you should explain clearly about your requirement. As i have understood, i think you want to access data which belongs to one activity in the other activity.
Refer the link below, you will get a clear idea.
http://developer.android.com/resources/faq/framework.html#3
u can use intents, bundles, or any other static variables to holds data from one activity to another

Transport own class with Intent between objects

HI,
i'm programming an android application using the StartActivityForResult() - Method.
But, i want to get as result an own class, not a string or something like that.
how can i tell the intent to use the class i wrote to give back?
You have to have your class implement Parcelable or Serializable. Also, this technique is only recommended for activities within the same application.

Categories

Resources