I am having a problem of how to call a method once when the condition achieves many times! for example:
public void onLocaitonChanged(Location location){
// this if statement may achieve the condition many times
if(somethingHappened){
callAMethodOnce();// this method is called once even if the condition achieved again
}
}
Please help with that
Maybe I don't understand your question correctly but from your problem definition I would suggest using a boolean variable something like.
Once the code under
if
statement gets executed oncerun
would betrue
and there won't be any subsequent calls tocallAMethodOnce()
You could simply set a flag. If you just need it to only happen once with each instance of the
Activity
then set a member variable.If you want it to only happen once ever in the life of the application then set that variable as a SharedPreference
set a class wide boolean