Transport own class with Intent between objects - android

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.

Related

How to start Activity with custom parameters

I need to start an Activity with parameters. I know i can do it with Intents and Bundles, but as far as i know it's only possible with String, boolean, etc...
I have to do it with custom parameters, like user-made classes.
For example, i got PLC-class in main activity, which contains many variables and methods, and i need to use it in the other activity.
I know the "public static" method, but i'm not a fan of it, and i'm sure i can find something else that fits my need.
I hope you guys can help me
Bye
Or you can make your class implementing parcelable interface, and put it in a parcel that you send from your source activity to destination activity..
In my opinion you can make bean class... which have getter/setter method.. you can set value there, and retrieve those values in any other Activities.Using it class.
E.g
public class Constants{
public static Bean userBeen=new Bean();
}
Main activity
Constants.userBeen.setValue("anything");
In other Activity,, you can get value using userBeenobj;
Other Activity
String s=Constants.userBeen.getValue();
Hope this helps to you. I mostly use this.!
make your custom made class serializable and then put it in bundle like this
bundle.putSerializable(key, value);

Sendind an Extra with my own kind of data

I need to pass an extra data to an activity, but I don't know how since my data is a variable from a specific class of mine. I have something like:
MyClass variable=new MyClass();
Intent intent= new Intent(this,SecondActivity.class);
intent.putExtra("variableName", variable);
I know the error in code is obvious, but I want to know how can I send data from a class of mine.
Please looking into Parcelable
How can I make my custom objects Parcelable?
http://developer.android.com/reference/android/os/Parcelable.html
You will have to serialize the data and then pass. Android doesn't support passing custom objects as extras. Look at this answer here. Passing custom objects between activities?

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

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

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.

Android Class Object problem

HI all,
actually i m new in android development.
i have one activity class and one simple class(Without Activity). in Simple class i m calling some web service and storing the response in array. after that this array i want to access in my activity class. m not able to make the object. how can i do that? i don't want to use bundle. as this array i want to access in more than one class.
please help me.
thanks in advance.
The activity calls/creates the Simple class, right? Pass a reference to the activity object to the Simple class, have the Simple class call the activity back with the data.
You can also use static, but it depends of data.

Categories

Resources