Just a bit about my app first. Its a quiz app. It has a main screen displaying a question, which loads straight away, and arrows going previous and next which go to other quiz questions. I’m using the same layout over and over, just by passing different question data, so when I click the “next” button, it will just launch an intent to the same class, just with different data. At the start of my one single layout, I have a little check to see if the user has clicked to hide the disclaimer or not.
So my disclaimer pops up at the start of the app. It has a dismiss button and a Dont show this again checkbox. I can get the checkbox working perfect, using SharedPreferences but the problem arises when they hit the Dismiss button. Since I’m reusing the same layout, any time the user navigates to a new question, the disclaimer pops up. I only want it to popup on the first screen, e.g. when the app loads.
I have tried setting another SharedPreference to hide the disclaimer when the user hits dismiss but once I hide it, it never comes back, because when the user loads the app back up again, that shared preference is still set. My problem is knowing where to set the preference back! I tried resetting it in onPause() but that didn’t work.
So, what I’m asking is, how can I determine if an activity is the first one to be loaded so I can only do the Disclaimer check then, and not in each of the subsequent screens?
Thanks.
EDIT: Here’s my OnCreate() method:
final static String disclaimerShownPref = "disclaimerShown";
final static String disclaimerShownOnce = "disclaimerShownThisSession";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
//Checks to see if the boolean is set
//The second argument is the default to use if the preference can't be found
if(!mPrefs.getBoolean(disclaimerShownPref, false))
{
if(!mPrefs.getBoolean(disclaimerShownOnce, true))
{
new Disclaimer(this);
}
}
topMostLayout=buildHomeScreen();
setContentView(topMostLayout);
}
In your case I would suggest using a static variable in your activity:
Having said that. I would consider a better approach to switch between questions in the same instance of your activity instead of creating a new one every time.
Good luck!