0

Is it possible to store an object on closing an activity and then retrieve it when you start activity again?

So the issue is following - I am developing an app which connects to Bluetooth slaves. I have a different functionality in different activities. I am able to connect bluetooth slaves to each activity, but when I move to different activity, the previous is destroyed. I would like to make them to work in parallel or at least continue previously established connection without need to reconnect.

So after connection I have a connection object. I was wondering if it is possible to store this object when I close the app and then when the app is ran again I check if this object exists and if it does, I am just using it.

Or do you have any other ideas how to overcome this problem and let those activities to run in parallel?

Thank you in advance.

Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76
  • u can use shared preference in that case..it will save – shassss Aug 06 '12 at 09:20
  • But do you think it can store an object? `getSharedPreferences(String, int)` – Arturs Vancans Aug 06 '12 at 09:22
  • "But do you think it can store an object?", no you can't. What are the contents of your object? If it has an active bluetooth connection storing the object will still lose the connection. If you just want to save the details of the connection using a parcelable (jsstp24n5's answer) will do the trick. – roflharrison Aug 06 '12 at 09:50
  • Mostly configuration flags, external API objects, callbacks – Arturs Vancans Aug 06 '12 at 09:53

3 Answers3

0

Manage your Bluetooth connection/s in a service.

You probably don't want to keep the connection running if the user navigates away unless you are doing something important, however using a service will allow you to keep the same connection/s while your application is alive.

roflharrison
  • 2,300
  • 23
  • 26
  • As far as I see it allows to create a service from a class. But what if I need multiple services with the same class? How I will be able to distinguish which is which because `onServiceConnected(ComponentName className, IBinder service)` it does not specify neither ID nor name, just class. – Arturs Vancans Aug 06 '12 at 09:51
  • You would have one service running in the background that would manage a collection of bluetooth connections. – roflharrison Aug 06 '12 at 10:15
0

Use Internal Storage to save it as a Parcelable (preferable) or as a Serializable:

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Is using Serializable in Android bad?

Community
  • 1
  • 1
jsstp24n5
  • 594
  • 1
  • 6
  • 13
0

I solved this issue relatively easy by using basic Java. I created a new static class as a connection table with simple setters and getters. onCreate I check if I have a connection object with my UUID stored in that static class and retrieve it. If I don't find, create one and add to the table.

Arturs Vancans
  • 4,531
  • 14
  • 47
  • 76