Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Different start activity depending on user preference

My application starts with a welcome screen Activity, but that screen has an option to skip that screen altogether in future launches.

What's the proper Android way to do this? Initially, I just automatically detected the skipWelcome preference and switched to the 2nd activity from Welcome. But this had the effect of allowing the user to hit the back button to the welcome screen we promised never to show again.

Right now, in the Welcome activity, I read the preference and call finish() on the current activity:

    SharedPreferences preferences = getPreferences(MODE_PRIVATE);
    boolean skipWelcome = preferences.getBoolean("skipWelcome", false);

    if (skipWelcome) {
        this.finish();
    } 

And then I implement onDestroy to move on to the next Activity:

@Override
public void onDestroy() {
    super.onDestroy();
    startActivity(new Intent(Welcome.this, StartFoo.class));
}

But this makes for some weird visual transitions. I'm starting to think that I need a base Activity that pops open Welcome only if proper, and then goes to StartFoo.

like image 939
Kevin Avatar asked Aug 27 '10 22:08

Kevin


2 Answers

I can't comment on Mayra's answer or I would (not enough rep), but that's the correct approach.

Hidden in the Android documentation is this important phrase for Activity.startActivityForResult(),

"As a special case, if you call startActivityForResult() with a requestCode >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your activity, then your window will not be displayed until a result is returned back from the started activity. This is to avoid visible flickering when redirecting to another activity."

Another important note is that this call does not block and execution continues, so you need to stop execution of the onCreate by returning

if (skipWelcome) {
    // Create intent
    // Launch intent with startActivityForResult()
    return;
} 

The final piece is to call finish immediately in the welcome activity's onActivityResult as Mayra says.

like image 63
John Avatar answered Oct 13 '22 11:10

John


There are a few solutions to this.

Did you try just launching the activity and finishing? I vauguely remember that working, but I could be wrong.

More correctly, in if(skipWelcome) you can start the new activity for result, then when onActivityResult is called, immidiately finish the welcome activity.

Or, you can have your launcher activity not have a view (don't set content), and launch either the welcome activity or StartFoo.

like image 27
Cheryl Simon Avatar answered Oct 13 '22 11:10

Cheryl Simon