Re: [android-beginners] Re: Facing serious problem with static objects

2010-01-23 Thread Alok Kulkarni
Thanks guys for the suggestions .. I will implement your suggested
solutions.. Uptil now i am using the logic what Coco has suggested.. Will
try out the other possibilioties ..
Thanks,
Alok.

On Mon, Jan 4, 2010 at 4:26 PM, swapnil kamble swap.kam...@gmail.comwrote:

 Hi Alok,
 if your both activities are from part of a single application
 then why don't you simply create a common non-activity class with members as
 static and access them from wherever you want.


 On Sun, Jan 3, 2010 at 12:17 AM, Coco meli...@superliminal.com wrote:

 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 HashMapString, String hMap;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 hMap = new HashMapString, 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 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 HashMapString, String hMap = new HashMapString,
   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,Alo);
   hMap.put(Jitu,Jit);
 
   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.comandroid-beginners%2bunsubscr...@googlegroups.com
 android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@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
 

[android-beginners] Re: Facing serious problem with static objects

2010-01-04 Thread Coco
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 HashMapString, String hMap;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hMap = new HashMapString, 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 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 HashMapString, String hMap = new HashMapString,
  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,Alo);
          hMap.put(Jitu,Jit);

          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.comandroid-beginners%2bunsubscr...@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


Re: [android-beginners] Re: Facing serious problem with static objects

2010-01-04 Thread swapnil kamble
Hi Alok,
if your both activities are from part of a single application
then why don't you simply create a common non-activity class with members as
static and access them from wherever you want.

On Sun, Jan 3, 2010 at 12:17 AM, Coco meli...@superliminal.com wrote:

 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 HashMapString, String hMap;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 hMap = new HashMapString, 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 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 HashMapString, String hMap = new HashMapString,
   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,Alo);
   hMap.put(Jitu,Jit);
 
   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.comandroid-beginners%2bunsubscr...@googlegroups.com
 android-beginners%2bunsubscr...@googlegroups.comandroid-beginners%252bunsubscr...@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.comandroid-beginners%2bunsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/android-beginners?hl=en




-- 

[android-beginners] Re: Facing serious problem with static objects

2010-01-03 Thread Nithin
-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 HashMapString, String hMap = new HashMapString,
  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,Alo);
      hMap.put(Jitu,Jit);

      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