1

I have two activities A and B.

public class A extends Activity 
{
....
@Override
    protected void onResume()
    {
    super.onResume();
    if(MyStaticVarz.myFlag)
    {
                    MyStaticVarz.myFlag= false;
        SomeTask();
    }

}

MyStaticVarz.java :

public class MyStaticVarz
{
    public static boolean myFLag = false;
}

Go from A to B and change myFlag to true like:

MyStaticVarz.myFlag = true;

and go back to A again,but in onResume if(MyStaticVarz.myFlag) is false and SomeTask() not reached.

Going from A to B like :

Intent i = new Intent(A.this, B.class);
startActivity(i);

UPDATE SomeTask() is for change fontsize of a text in A and B.

myFlag is for on demand reinitialize of UI that if font setting changed,then SomeTask() run.

When click on optionMenu in B,and change font size,and go to B,i see changes,but when go back to A,text font size not happen.

Maybe Important: when i'm back to A and font size is not ok and myFlag is false too,if i change oriantation,text fontsize is ok but myFlag is false again!

Dr.jacky
  • 3,341
  • 6
  • 53
  • 91
  • Can you add the activity which tries to change the static field. – Serdar Dogruyol Nov 11 '12 at 08:05
  • @Dr.jacky Do you finish Activity `A` when go to Activity `B`? – hasanghaforian Nov 11 '12 at 08:09
  • @hasanghaforian Please see question again.it's updated. – Dr.jacky Nov 11 '12 at 08:27
  • @SerdarDogruyol Believe me there is nothing in B when change myFlag. why?because of SomeTask() run in B . – Dr.jacky Nov 11 '12 at 08:48
  • Where exactly is this line in your activity lifecycle? MyStaticVarz.myFlag = true; – Simon Nov 11 '12 at 08:48
  • @Simon Please see question again,Updated. – Dr.jacky Nov 11 '12 at 09:05
  • You have also added another line to your onResume() since your OP. This is getting a bit messy. I think you should rethink the question and try again, with a new question including all of the relevant code (anything that sets or gets myFlag). Despite your question title, I can assure you that static variables work just fine. However, it seems to me that this question is actually about activity life cycle and not some suspicion that there is a major bug in Android. – Simon Nov 11 '12 at 09:27

4 Answers4

5

If you access your "myFlag" variable from different threads, each may have a locally stored copy, so a change in the variable's value might not be seen by different threads immediately.

Define your variable as volatile to force all threads to see the same copy:

public static volatile boolean myFLag = false;

See this nice answer for a more detailed explanation

Community
  • 1
  • 1
Twilite
  • 873
  • 9
  • 22
  • +1 - The OP certainly needs to do this, but it is not entirely clear that this is the cause of the problem. The question is missing vital details ... – Stephen C Nov 11 '12 at 08:13
2

Because you are accessing a single static variable in your code from 2 threads you need to add some locking around that access.

I would look at something like:

public class MasterBB extends Activity 
{
  private final ReentrantLock lock = new ReentrantLock();

  @Override
  protected void onResume() {
    super.onResume();
    lock.lock();
    try {
        if(MyStaticVarz.myFlag) {
            MyStaticVarz.myFlag= false;
            SomeTask();
        }
    }
    finally {
        lock.unlock();
    }
  }
}

I'm afraid my Java is a bit rusty, there might be a more up to date way too do it, but this is the general idea. You need to ensure that myFlag does not get the chance to get modified while that block of code is running.

Steve
  • 8,469
  • 1
  • 26
  • 37
  • Declaring the variable as volatile does not implement locking on the call to `SomeTask()` for you, it only ensures synchronization on access to the variable itself. – Steve Nov 11 '12 at 09:27
0

Is false the initial value of the member? Perhaps your application's variables are being reset during the process. If I remember Android correctly, onResume can be called as a part of app termination, which I am almost certain destroys all of your non-stored data. You'd need to store that information in a Bundle and restore from it as well.

Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
  • @Chris. Sorry, but with your rep should be getting into the habit of being precise. "application variables are being reset" - this can only happen if you reset them. There are no circumstances in which Android "resets" variables (and there is no such thing as "reset", I assume that you mean reinitialised). onResume() is never called as part of "app termination" nor is it when an activity is destroyed. I think you might be getting confused between apps and activities and the relationship between activity instances and the app but your answer is so vague and fuzzy, it's hard to tell. – Simon Nov 11 '12 at 08:46
0

Your answer is simple, when you change MyStaticVarz.myFlag to true in getView, then you back to activity B, in onResume() the value of MyStaticVarz.myFlag change from true to false and when you back to activity A , the value of MyStaticVarz.myFlag is false not true and is obvious that activity A never get true value.

To solve this problem you must save changed value :

String value = entryValues[index].toString();

to static String in MyStaticVarz and in activity A and B onResume() event check the static String with local String to understand change.

CooL i3oY
  • 790
  • 1
  • 9
  • 26