Sharing data across Activities

by Tushar » Fri, 13 Mar 2009 21:48:16 GMT


Sponsored Links
 Hi,
I was looking out for ways to share data between various activities
that one may need to create for his applications . Using "Parcel"  and
Bundle one can do data exchange , but looks like  this is a bit
cumbersome process specifically if you have to share large array
objects with too many fields.

As  android.app.Application maintains global state for the
application , we can use this class to store global data and access it
across activities.  Is this a good way ? Are there any implications on
allocating large objects in the your class which extends from
android.app.Application ?

Is there any other better approach available ?

--Tushar




--~--~---------~--~----~------------~-------~--~----~



Sharing data across Activities

by nagaraj attimani » Fri, 13 Mar 2009 22:31:29 GMT


 Content Provider might sortout this issue ..






-- 
Best Regards
Nagaraj

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


Sharing data across Activities

by Tushar » Fri, 13 Mar 2009 22:57:37 GMT


 But as per the Android documentation "A content provider is only
required if you need to share data between multiple applications".

--Tushar


On Mar 13, 3:31pm, nagaraj attimani <nagaraj.attim...@gmail.com>



--~--~---------~--~----~------------~-------~--~----~



Sharing data across Activities

by Edward Falk » Fri, 13 Mar 2009 23:00:26 GMT


 Are the activities all in the same application?  Same task?  It seems
to me that you could just store the data in a static variable
accessible by all of the activities.  Am I missing something?

public class MainActivity extends Activity {
  ...
  static MyDataStructure dataStruct = null;
  ...
  public void onCreate(Bundle state) {
    ...
    dataStruct = ReadData();
  }
}

...

public class AnotherActivity extends Activity {
  ...
  void SomeMethod() {
    DoSomethingWithData(MainActivity.dataStruct);
  }
}
--~--~---------~--~----~------------~-------~--~----~



Sharing data across Activities

by Tushar » Fri, 13 Mar 2009 23:10:28 GMT


 Yes my Activities are all in the same application. I'm not sure about
"Task".

Although I'm not killing my one activity (say activityOne )when
launching another activity ( say activityTwo) but are there any
chances that Android might simply kill my activityOne and I may loose
the data which I'm expecting in  activityTwo by storing it as static ?





--~--~---------~--~----~------------~-------~--~----~



Sharing data across Activities

by Dianne Hackborn » Sat, 14 Mar 2009 01:07:16 GMT


 Yes this is very dangerous.  There is a good chance that if the user hits
home, and later returns to your app, the process will have been killed and
all static data in it lost.  This is very unlikely to happen when just
switching from one app to another (the process in that case stays in the
foreground bucket), but still I am not going to make any guarantees.







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support.  All such questions should be posted on public
forums, where I and others can see and answer them.

--~--~---------~--~----~------------~-------~--~----~



Re: Sharing data across Activities

by DebUggEr » Wed, 09 Mar 2011 22:31:19 GMT


 Seems like heres the answer. I am not an expert but do consider and let me 
know too.

ANDROID.APP.APPLICATION CLASS

Each Android application can have at most one android.app.Applicationassociated 
with it. You are responsible for sub-classing the Application Class, and it 
is used to maintain a global state of the application across all Activites. 
Conceptually, you can think of it as a non-static singleton its life cycle 
being managed by Android OS.
*Benefits:*

   - Gives you complete control over the management of application 
   life-cycle, so resources can be properly initialised and disposed of.
   - Provides a single entry point where *any* Activity or Service within 
   the application can gain access to the desired object

*Drawbacks:*

   - Can only expose information to Activities and Services within the 
   application

-- 



Re: Re: Sharing data across Activities

by Dianne Hackborn » Thu, 10 Mar 2011 01:15:15 GMT


 That description is pretty misleading.  It doesn't give you complete control
over management of application lifecycle, and I really have no idea what it
means in the second bullet point (you could just as well do what I think it
is describing using a static global).







-- 
Dianne Hackborn
Android framework engineer
hack...@android.com

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.

-- 



Other Threads

1. OOT: Happy Birthday Bro Dedi Sopian

kita pisahin email aja ya biar gak bajak thread

Happy birthday buat bro dedi sopian.
Semoga makin sukses dalam berbisnis dan sekeluarga diberi kesehatan selalu'

Wish you all the best bro :D

-- 
===============
"

2. OOT : tanya jaringan smart denpasar

siang.......

tanya dong, ni sedari pagi inet pake smart di incredible bengong bloon hehhe

apa emang maintenis, apa gangguan jaringan apa apa??hehe

mohon pencerahan klo ada yg bisa menerangi

trims, salam

-- 
===============
"

3. Question about authentication certificate

4. [WTA] setting parameter via cdma workshop dan QPST selalu mbalik

5. HTC Desire HD Maps video

6. (ASK) MOHON BANTUAN SE X10 MINI PRO

7. <WTA>upgrade LG optimus