I'm trying to do something using a boolean
in a Fragment
class each time the Fragment
is displayed.
Example
My app launches, opens the FirstFragment
and the boolean
for the first time is always true
, then I have an if
clause that checks its value:
if (FirstTime) {
FirstTime = false;
} else {
// Other stuff here, cause it's not true.
}
Then, on the first time, when FirstTime
is true
, I do stuff like go to another Fragment
. and when I return to Fragment1
and on my onCreate()
, I do the same. It's always true
, seems that it's refreshing or something.
Then I thought that could be a problem with Fragment
, and every time I press on Fragment1
, it restarts or something. Then, I've added a getter and setter in my MainActivity
:
public Boolean getFirstTime() {
return FirstTime;
}
public void setFirstTime(Boolean FirstTime) {
this.FirstTime = FirstTime;
}
where since the start, it's true and then, I changed my code from Fragment1
for:
if (((MainActivity) getActivity()).getFirstTime())
((MainActivity) getActivity()).setFirstTime(false);
} else {
// Other stuff here, cause it's not true,
}
However, it's still saying that's true.
What I'm doing wrong or what I misunderstood about Fragments?
Is there any way to do it?