His code as given would crash if initialized to null on activity
change. I think what you mean is to reinitialize the containers which
is what I would suggest trying. Something like this:
public class MyActivity extends Activity {
public static HashMap<String, String> hMap;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hMap = new HashMap<String, String>(); // Recreate container
each time.
...
}
Alternatively he can just call hMap.clear() in onCreate() which will
have much of the same effect. Which approach is better depends on
exactly what he's trying to do.
Another thing to consider is whether these members really want to be
static in the first place.
-Melinda
On Jan 2, 8:50 am, Yousuf Faheem <[email protected]> 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 <[email protected]> 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
> > [email protected]<android-beginners%[email protected]>
> > 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
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en