1

I have asked this question a few times on here already, but still am not confident enough to start throwing things into my code because I'm afraid I'll ruin the functionality it already has.

What I want to do is have a Bluetooth connection with a device that I can send/receive data to/from, and be able to access it from several different Activities. I know Intents can be used, but can anyone try to explain how intents work?

I don't understand how if I start with an Activity, how I can build an object within that Activity, then end it, and still have the object. When I call the intent in the following Activity, how do I access it?

I can not find example code of an Activity with a custom object that is passed through an intent, opened in another Activity, then passed again through another Activity, and so on. All I can find is things for string's, int's and it seems that those methods are hardcoded for the specific objects.

Any help would be greatly appreciated, thanks.

JuiCe
  • 3,884
  • 16
  • 62
  • 114
  • 2
    [use this](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-in-android) – Praveenkumar Jun 15 '12 at 13:46
  • possible duplicate of [Passing data between activities in Android](http://stackoverflow.com/questions/2965109/passing-data-between-activities-in-android) – Idolon Jun 15 '12 at 14:43
  • I didn't think its a duplicate because I specified my problem a bit more. I'm not just passing data, I'm trying to pass a custom class over and over and over again. The other thread talks about passing a string from activity #1 to activity #2 and thats it. – JuiCe Jun 15 '12 at 14:54

3 Answers3

2

If you want to pass a custom object from an Activity to another one, your object have to implement Parcelable interface.

Here you can find an example of an object which implements Parcelable. Once you have an instance of this object you can add it to an intent and pass it to other activity.

Let's say you have 2 activities: A and B. If you want to send a custom object from A to B try put your parceable object in an intent and get it back in Activity B.

// Activity A
Notebook myNotebook = new Notebook(); // the "Parcelable" object
Intent intent = new Intent(A.this, B.class);
intent.putExtra("object", myNotebook);
startActivityForResult(intent);

// Activity B
// in onCreate method
Notebook notebook = intent.getParcelableExtra("object");
Ungureanu Liviu
  • 3,824
  • 4
  • 35
  • 42
1

there is a good reason you don't find a good example of transfering complicate objects: you don't suppose to that a lot. it's possible to transfer with intent extra any object, with declare the class you want to transfer as implements Serializable, or Parcalable.

but - this is not recomended doing so, unless you have good reason. there are unlimited ways to create "cross activities" objects (databases, singeltones, services..), and passing complicated objects in intent is heavy operation. also it makes terrible performances doing so.

anyway - if you want anyway doing so, that's the way:

this is how to pass serlizable object:

Intent intent =  new Intent();
    intent.putExtra(name, someInstanceOfClassWhichImplementsSerializableInterface);

this is how to acheive it on the other activity:

getIntent().getSerializableExtra(name);

about the question that you don't understand how the object still lives after you finish the activity - finish() activity is not triggering distractor!!!, but do triggers to onDestoy() callback. the system decides when it's the right time call the activities distracor , and even if it was distractor - it's not preventing from the Intent object to stay alive - because nobody said your activity is the only one holding reference to it. the intent reference held by the system for the perpose of passing it to the new activity which opened with it.

Tal Kanel
  • 9,855
  • 10
  • 55
  • 93
  • THank you, do you think singleton the singleton method is a better approach for my problem? It seems like that is the best alternative to intents, and I'm not comfortable with intents, it seems they aren't the best option. – JuiCe Jun 15 '12 at 14:11
  • 1
    singeltone class which holds shared data can be a good option. but if your class is not too complicated/big - passing it as Serializble not that terrible. just keep in mind it's not recommended passing complex big objects in that way – Tal Kanel Jun 15 '12 at 14:14
  • If I use the singleton method, do I still use intents to start new Activities? I think the singleton method will work best for me, as it requires me learning the least amount of new information. – JuiCe Jun 15 '12 at 14:30
  • Not to flood you with questions, but if I use the singleton method, from what I've read I want to make an original class which extends Application. Where do I initialize this Application object? – JuiCe Jun 15 '12 at 14:36
  • 1
    user1435712: there is no connection between singeltone design pattern to intents, so I didn't understand why you asked that. anyway - there is no way starting activity without intent. that's the only way – Tal Kanel Jun 16 '12 at 05:23
  • 1
    @user1435712: these is link for example how to use the application class. http://intridea.com/blog/2011/5/24/how-to-use-application-object-of-android I don't see why you need to do such a thing. you can read in android developers that they are recommending doing singeltone, and not extends class application - http://developer.android.com/reference/android/app/Application.html – Tal Kanel Jun 16 '12 at 05:27
  • I understand that now. My first understanding of Intents was that they passed data from class to class, I now know that is not correct. And I'm sorry, I think I may be misunderstanding you but in the above comment you say to use the singleton method, without the use of the Application class. But then you linked me to the Application docs? – JuiCe Jun 18 '12 at 12:11
  • @user1435712: that's right, but I linked you to the application class in android's developers to show you they are writing there it's better to use singeltone classes for situations like yours, unless there is some special reason (which you not have). so - Android framework developers recommends using singeltone classes to store global application data, and more then that - the recommendation written on the "Application" class documentation!!! – Tal Kanel Jun 18 '12 at 16:39
1

Using intent you can send serialized object which is like converting you object into byte stream. If you want to send your object as it is you can use handlers. e.g.

  1. create Bluetooth connection using service(android service)
  2. define handler in you activity which will be static object
  3. from service to send the object add to msg.obj send that msg to handler
meager
  • 209,754
  • 38
  • 307
  • 315
Vishal Pawar
  • 3,843
  • 3
  • 24
  • 53