Question

There are posts that touch on this question, but no one that I can find that really directly approaches it or solves it.

I have a main Activity that I want to call/create a help screen (it is really a second activity) when the user first gets into the app.

But, for users who are familiar with the app, I want to give them the option of not having that help screen every time at startup.

So, I set a preference in a SharedPreferences file. All the code works perfectly, including the SharedPreferences stuff (I can check this by looking at the CheckBox I have set up in the Settings screen I made to allow the user to opt out (or later in) to the opening help screen.)

Here is the code I have at the beginning of my onCreate() (override) method:

spSettings = getSharedPreferences(strPrefsFilename, 0);
bHelpOnStart = spSettings.getBoolean(strHelpParamName, true);

Then, I use simply:

if (bHelpOnStart)
{
    // Show help screen.
}

The problem is that onCreate() for my main Activity is called every time I return from some other activity! I want my test for whether the help screen gets displayed to occur ONLY when the user comes to the app from "outside" it, specifically, from the Home->apps page.

Is there a method in Activity that is ONLY called when the Activity is come to from "outside" in the sense I just mentioned?

Thank you!

Was it helpful?

Solution

Possible solution:

In application class:

public static boolean firstStart = true;

In Activity class:

onCreate(){
   boolean firstStart = ((MyApplication)getApplicationContext()).firstStart;
   if (bHelpOnStart && firstStart) { startHelp(); }

   ((MyApplication)getApplicationContext()).firstStart = false;
}

Note this is not a good coding style, (better with singleton), but you should get the idea. Of course, if your application stays in memory and user open it again from launcher, it won't display the firstStart; but at least when it go back from one activity (of your app) to the main one, it won't show the help again. There's no easy way to determine who starts your activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top