0

The code below I have issue, I have more than one class, in example2 class have static boolean var

public class example2 extends Activity {
    public static boolean var = false;
}

In example1 have static variable reference from example2.

public class example1 extends Activity {

    public void onResume() {
        super.onResume();
        example2.var = false;
    }
} 

Bug: Write to static field com.example.example2.fromVar from instance method com.example.example2.onResume()

How to resolve this one...

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
Manoj
  • 3,947
  • 9
  • 46
  • 84
  • See http://stackoverflow.com/questions/4878159/whats-the-best-way-to-share-data-between-activities. Also see http://stackoverflow.com/questions/24703755/write-to-static-field-from-instance-method, which explains why findbugs thinks this is a bad practice. Since you're working with Android, though, I think you need an Android-specific solution. – ajb Mar 23 '16 at 05:44
  • Use shared preferences to save values. – Dhinakaran Thennarasu Mar 23 '16 at 05:49
  • actually the description of the bug has nothing to do with example1, you probably do the writing in one of the example2 methods. in example2.onResume() apparently – Pooya Apr 09 '16 at 08:09

1 Answers1

0

Well, writing to a static field from a non-static context is a code-smell, Why you do it this way? Fix is simple, don't write to a static variable, but it is hard to help when we don't see all of your code.

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115