->Output First time is Size = 0. for 1st launch This is because after initializingm, you are checking the size of hashMap.
->Next for every launch , HashMap size is 2... Now, you already inserted two values to hashMap.HashMap takes only unique keys. So for second time onwards, it won't insert again to hashMap. ->If i use ArrayList , the size of ArrayList goes on increasing. For arrayList, there is no key. So it keeps on adding values to the list. ->One thing was that to clear the HashMap in onDestroy() or in onStop() in case user presses Home button. When user presses home button activity will call onPause() and onStop (). So you modify according to your requirement. Just check the activity LifeCycle, you will get more idea. Nithin On Jan 3, 8:05 pm, Sean Hodges <seanhodge...@googlemail.com> wrote: > I've never been a fan of using static fields for passing data between > activities, partly because of the kind of problems you hit already. > The lifecycle of an Activity is not directly associated with the > lifetime of an object on the JVM heap. > > You might find passing your hashmaps/arrays to each activity using the > Intent extras mechanism more intuitive for your purposes. That is > assuming the objects you are passing around can be made Serializable. > > Details on data passing options can be found > here:http://developer.android.com/guide/appendix/faq/framework.html#3 > > On Sun, Jan 3, 2010 at 1:35 PM, Alok Kulkarni <kulsu...@gmail.com> wrote: > > I have temporarily found out a workaround for it. Initializing the arrays > > and hashmaps to null at change of activity wont work as i have many > > activities and all of them need to share these Data structures. > > Thanks, > > Alok > > > On Sat, Jan 2, 2010 at 10:20 PM, Yousuf Faheem <yousuf.syed....@gmail.com> > > wrote: > > >> Hi, > > >> Try initiallizing them to null before the change of activity takes place > >> or at the start of home activity. Since your application is not > >> closed(scope > >> of the static objects), the static objects are there in memory allocated to > >> that process and hence retain the value that you give. > > >> Thanks & Regards, > >> Yousuf Syed > > >> On Sat, Jan 2, 2010 at 2:27 AM, Alok Kulkarni <kulsu...@gmail.com> wrote: > > >>> Hi guys, I am facing a problem in my application. > >>> I have a few static arrays and Hashmaps in my app which i fill in when my > >>> Activity starts.Then this activity later launches another Activity. Now > >>> user presses Home button of Back button and again restarts the app, even > >>> then the static objects remain in memory. I beleive this is default > >>> android > >>> behaviour. > >>> I have an example app. > >>> package com.cm.test; > > >>> import java.util.HashMap; > > >>> import android.app.Activity; > >>> import android.os.Bundle; > >>> import android.util.Log; > > >>> public class MyActivity extends Activity { > >>> public static HashMap<String, String> hMap = new HashMap<String, > >>> String>(); > > >>> /** Called when the activity is first created. */ > >>> @Override > >>> public void onCreate(Bundle savedInstanceState) { > >>> super.onCreate(savedInstanceState); > >>> setContentView(R.layout.main); > >>> Log.i("***","Hash map size = "+hMap.size()); > > >>> hMap.put("Alok","Alokkkk"); > >>> hMap.put("Jitu","Jituuuu"); > > >>> Log.i("***","Activity onCreate() called"); > >>> } > >>> @Override > >>> public void onDestroy() > >>> { > >>> Log.i("***","Activity onDestroy() called"); > >>> super.onDestroy(); > > >>> } > > >>> @Override > >>> public void onResume() > >>> { > >>> super.onResume(); > >>> Log.i("***","Activity onResume() called"); > >>> } > >>> @Override > >>> public void onStop() > >>> { > > >>> super.onStop(); > > >>> Log.i("***","Activity onStop() called"); > >>> } > > >>> } > >>> Output First time is Size = 0. for 1st launch, Next for every launch , > >>> HashMap size is 2... If i use ArrayList , the size of ArrayList goes on > >>> increasing. > >>> I dont want this to happen.One thing was that to clear the HashMap in > >>> onDestroy() or in onStop() in case user presses Home button. But my > >>> Activity > >>> launches another ListActivity in it. So the alternae option is to clear > >>> the > >>> Hashmaps in onDestroy() | onStop() of this activity. > >>> I am not much clear when the Activity exits. From the posts that i have > >>> read i found few things here > >>>http://groups.google.com/group/android-developers/browse_thread/threa... > >>> The manifest part android:clearTaskOnLaunch=true does not work in my > >>> case. > >>> What do you people suggest in this case? > >>> Thanks, > >>> Alok. > > >>> -- > >>> You received this message because you are subscribed to the Google > >>> Groups "Android Beginners" group. > > >>> NEW! Try asking and tagging your question on Stack Overflow at > >>>http://stackoverflow.com/questions/tagged/android > > >>> To unsubscribe from this group, send email to > >>> android-beginners+unsubscr...@googlegroups.com > >>> For more options, visit this group at > >>>http://groups.google.com/group/android-beginners?hl=en > > >> -- > >> You received this message because you are subscribed to the Google > >> Groups "Android Beginners" group. > > >> NEW! Try asking and tagging your question on Stack Overflow at > >>http://stackoverflow.com/questions/tagged/android > > >> To unsubscribe from this group, send email to > >> android-beginners+unsubscr...@googlegroups.com > >> For more options, visit this group at > >>http://groups.google.com/group/android-beginners?hl=en > > > -- > > You received this message because you are subscribed to the Google > > Groups "Android Beginners" group. > > > NEW! Try asking and tagging your question on Stack Overflow at > >http://stackoverflow.com/questions/tagged/android > > > To unsubscribe from this group, send email to > > android-beginners+unsubscr...@googlegroups.com > > For more options, visit this group at > >http://groups.google.com/group/android-beginners?hl=en -- You received this message because you are subscribed to the Google Groups "Android Beginners" group. NEW! Try asking and tagging your question on Stack Overflow at http://stackoverflow.com/questions/tagged/android To unsubscribe from this group, send email to android-beginners+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en