1

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..

Community
  • 1
  • 1
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166

3 Answers3

1

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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • i tried like this but no use. look at my Question i was edited.. – RajaReddy PolamReddy Jul 19 '12 at 10:43
  • @RajaReddyP : can you tell me what is `client` and where you read we can only pass strings between Activities using bundle or intent?.as i known we can pass any object or data which implements Parcelable and Serializable interfaces – ρяσѕρєя K Jul 19 '12 at 10:47
  • Actually my requirement is different, but i confused about the objects i said like that. now i was edited my question, now you can understand.. – RajaReddy PolamReddy Jul 19 '12 at 11:15
  • @RajaReddyP : can you past me XmmppClient apiu docs link – ρяσѕρєя K Jul 19 '12 at 11:18
  • Xmpp api are aSmack library, you can get this in google, just search xmpp smack library. – RajaReddy PolamReddy Jul 19 '12 at 11:22
  • @RajaReddyP : only solution for your problem is Application class see this http://stackoverflow.com/questions/4208886/using-the-android-application-class-to-persist-data example how we use Application class for sharing an object or any other data to all activities – ρяσѕρєя K Jul 19 '12 at 11:27
0

Yes, its possible only if your custom object class implements Parcelable interface.

waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • by using parcelable i can pass string, int of custom values, here there is no custom vales it's function. – RajaReddy PolamReddy Jul 19 '12 at 10:43
  • You dont use **Parcelable** to send string, int and other literal values, using **Bundle** is enough for that. You cant pass methods, its only objects which can be passed. Methods are called only. – waqaslam Jul 19 '12 at 10:45
  • look at my Question i was edited now – RajaReddy PolamReddy Jul 19 '12 at 10:46
  • In your case, i think you need to use [Observer/Observable](http://www.javaworld.com/jw-10-1996/jw-10-howto.html) approach to call methods of an object instance from another class without having reference to it. – waqaslam Jul 19 '12 at 10:47
  • Actually my requirement is i want to keep live one function throughout the application, according to iPhone knowledge i called like that..what is the solution for this.. – RajaReddy PolamReddy Jul 19 '12 at 10:56
  • then you should override Application class and define it in you AndroidManifest.xml. By this way, you can easily get reference of your application class (using getApplicationContext) and include all the custom methods you need in that class – waqaslam Jul 19 '12 at 11:01
0

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");
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61