0

I have an application which has 3 activities.

  • Activity1
  • Activity2
  • Activity3

When the user opens the application, I am fetching the user's location and storing it in a static variable in a Helper class. Lets say the variable is latlng. Now I need to access the value of latlng variable in Activity2. So i simply use Helper.latlng in Activity2 . It works fine most of the time.

But if I press the home button and open some other application, and then come back to my application, my application force closes. After debugging I find out that latlng variable is becoming null in Activity2.

So, after going through various documentations, I came to find out that if a lot of applications are running, then Android OS may kill some applications. So basically if I open some other application, my current activity is destroyed by the OS. So automatically all my static variables are gone.

One way to overcome this is continue using static variables with onSavedInstanceState method which is called when the activity is getting destroyed.

   public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        LatLng latLng  = Helper.latlng;
        outState.putParcelable("latlng",latLng);
    }

And retrieve it using

 if( savedInstanceState != null ) {
            Helper.latlng = savedInstanceState.getParcelable("latlng");
        }

I am really confused whether I should go with the above approach. Is there any other way to use a variable across different activities in Android. I can from my experience say that using static variables is not a safe bet.

l-l
  • 3,804
  • 6
  • 36
  • 42
user3213851
  • 1,068
  • 2
  • 12
  • 24
  • 2
    how about using SharedPreferences? – Sascha Kolberg Jul 27 '15 at 18:08
  • "I can from my experience say that using static variables is not a safe bet" Yes it is not a safe bet. Except for final constants, don't use them. Checkout state restoration methods like what you've described. Or go with [some persistent storage options](http://developer.android.com/guide/topics/data/data-storage.html). – Ahmed Khalaf Jul 27 '15 at 18:11

3 Answers3

1

Just to get the most important part out of the way: You should NEVER use static variables in Android unless you know exactly what you are doing. Otherwise you are going to create memory leaks and a whole other slew of problems.

If you want to pass data to another Activity you should use Intents.

Method calls always have additional overhead. Accessing a field directly will always be faster. But this does not mean that you should abandon getters and setters all together.

For example look at this class Example:

public class Example { 

private String text;

public String getText() {
    return text;
} 

public void setText(String text) {
    this.text = text;
} 

public void doSomething() { 
    ... 
} } 

Inside doSomething() you should work with the field directly. Calling the getters and setters in there would not be the way to go since that would require additional overhead each time you call one.

If you are using the Example class inside another class like below you should use the getter and setter methods: Example instance = new Example(); instance.setText("some text"); ... String text = instance.getText();

Making the field public and accessing it directly like instance.text would be bad.

BUT any modern device has so much computing power that the overhead generated by a method call is completely negligible. The DOC do state that you should avoid using getters and setters in performance critical places, but that rule is from a time when most Android phones had 128 MB RAM. The main reason you should adhere to the rules I explained above is just one thing: coding style.

Your first concern should always be to write clean, readable and maintainable code.

Adarsh Yadav
  • 3,752
  • 3
  • 24
  • 46
0

One of the ways of doing it is using the SharedPreferences storage in android.

SharedPreferences pref =
getApplicationContext().getSharedPreferences("LocationPrefs", MODE_PRIVATE); 

Editor editor = pref.edit();
// Save latLang as a string variable
editor.putString("latLang", "string value of latLang");  

// Save the changes
editor.commit();

You can retrieve the results in this way,

String latLang = pref.getString("latLang", null); 
Aruna Tebel
  • 1,436
  • 1
  • 12
  • 24
  • do you really think using sharedpreferences is a good idea ? What about the user presses the home button, clears the application data and comes back to the activity. This is not reliable. – user3213851 Jul 27 '15 at 18:23
  • In that case even if you are using a database system to persist the required data, you lose it when user clears application data. My opinion is using a storage mechanism like `SharePreferences` is better than a static variable in your case. – Aruna Tebel Jul 27 '15 at 18:30
  • If you can look into [this](http://stackoverflow.com/questions/9529302/what-is-more-efficient-static-data-passing-shared-preferences-database) question. – Aruna Tebel Jul 27 '15 at 18:32
0

There are few ways of passing data between Activities.

  1. SharedPreferences
  2. SQLite
  3. Saving to file
  4. Intent with putExtra

In Android environment user can always delete app data, so you need to be prepared for that anyway

jakubbialkowski
  • 1,546
  • 16
  • 24