Garima Tiwari
Garima Tiwari

Reputation: 1540

How to clear values of all static variables at the end of an activity Android?

I have created an application that takes in user name and other details for a transaction and then fills them in a database. At times the application shows odd behavior by filling the SAME details in the database twice as two transactions. Even though the new values are read but not STORED in the static variables.

Therefore I needed help in flushing the values of all my static variables at the end of each activity to avoid overriding of the previous values in a fresh transaction.

EDIT :

  public class One
      {
  static String var;

      public void onCreate(Bundle savedInstanceState)
    {    
      super.onCreate(savedInstanceState);
              var="blah";
         }
   }

          public class Two
            {
              static String variable = One.var;
               // This is where i am accessing the value of the variables from previous activities.

              //CODE
            }

Upvotes: 1

Views: 3530

Answers (1)

Karan Mavadhiya
Karan Mavadhiya

Reputation: 1052

May these help you..

Using Static Variables is like a nightmare in any activity as it stores memory through out the activity..

I think you can try some other memory store to overcome your problem of passing value from one activity to another..

In my opinion u can store values in SharedPreference or either you can pass value through intent to other activity where ever it is required..

Hope these will help you..

EDIT:

Intent in = new Intent(MainActivity.this,SecondActivity.class);

You can use more than one putExtra() method to put several values and can fetch then in Second Activity

in.putStringArrayListExtra(String name, ArrayList<String> value);

StartActivity(in);

In Second Activity:

Intent in = getIntent();

ArrayList<String> Roleids = new ArrayList<String>;

RoleId = in.getStringArrayListExtra(String name, ArrayList<String> value)

Upvotes: 5

Related Questions