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");
Related
I have:
public MissionActivity extends AppCompatActivity {
public SoundPlayer getSoundPlayer();
public DiffrentSoundPlayer getDiffrentSoundPlayer()
//...etc.
}
I am passing this Activity in constructor to another class that need all
this soundplayers and assetManager. All these players need to be created in activity. My question is: is this a good practice in Android to pass reference to Activity to another object?
Passing the activity is not in itself problematic, but given the scope of activities is something that should be done thoughtfully.
Depending on your specific case I would consider wrapping the objects that you have created on the activity inside a data transfer object and pass that object to the class that needs the objects. And by "data transfer object" I simply mean an object whose sole purpose is to store the data/objects that you are transferring.
But simply passing the activity to the other class is also completely okay, as long as you don't store the activity in that other class (just store the objects that you need from the activity).
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
I need to pass an object between 2 activities that have no connection between them (meaning, neither of them calls the other).
My Main_activity extends TabActivity. I have 2 tabs : CurrencyList (extends ListActivity implements OnItemSelectedListener) and CurrencyCalculator extends Activity.
I also have class currencyData that saves data about different currencies.
In the CurrencyList activity I created a new currencyData object and initiate it with data.
How can I pass it also to the CurrencyCalculator activity?
2 quick ways:
1. use an static method in your activity to retrieve current ticket id
2. Design and implement an interface and register the fragments as listeners from the activity
Static Method is preferable for large data.
If there is absolutely no connection between them, then one method of accessing data in different Activity classes would be to declare the data members as static class members. Keep in mind that static objects from Activities persist even after you destroy the activity and Android keeps this around for some time even after the you leave the application.
These might not be the easiest ways (just some alternate approaches to the two answers given). You can use a Handler or a BroadcastReceiver to pass the data to the other activity through an intent.
Note
that your object would have to implement either Serializable or
Parcelable if you want to pass it through an intent. I have used
Serializable before and as long as your object does not have any
nested custom objects, you actually have to do no additional work.
Also the assumption is that the receiving activity is alive and is
able to receive the broadcast and/or message.
Another approach would be to write the object to a file (again it would need to be serializable) and read it back in the other activity.
I'd like to pass an entire (custom) class reference to another activity.
This class is called WeekProgramData. WeekProgramData has an array of 7 Day (class) instances and each Day class has several Switch instances.
How can I pass along the class reference between activities, so I can use the methods from that class etc. in other activities?
I already tried the following, but it failed: First, declaration in Activity A
WeekProgramData wpd = new WeekProgramData();
Code for passing the WeekProgramData class reference in activity A:
Intent intent = new Intent(v.getContext(), WeekOverview.class);
intent.putExtra("wpd", wpd);
startActivity(intent);
Code in Activity B for getting the class reference:
Bundle extras = getIntent().getExtras();
WeekProgramData wpd = extras.getWeekProgramData("wpd");
To pass a class via intent you have to implement the Parcelable interface. Here is a tutorial on how to do it and it shouldn't take you too long. Once you do this you can pass instances of that class via an intent to whatever you want.
The way to pass custom classes between activities is to make your classes Parcelable. This is a way of serializing your data so it can easily be sent around. Once your classes are Parcelable you can use the standard putExtra and pass your class in. To retrieve your class you can use getParcelableExtra. With Parcelable remember you cannot parcel null values and all nested classes must be parcelable as well. In your case you will have to make your WeekProgramData class parcelable as well as your Day and Switch classes.
There could a couple of diiferent ways to do this, but the two best options in my opinion are:
WeekProgramData should implement Parcelable
or convert wpd into String using Gson, pass that string to your second activity and finally reconstruct your object from that string using Gson again
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.