6

I am designing an android application where I need to add the splash screen of my application. Generally I used to use only Activity upto till now but for this project ADT is creating the Fragment also with Activity.

Now I have a confusion where I should write code of timerTask and Timer to schedule a task to perform either in onCreate of the Activity or onCreateView method or something else ?

Currently I have written like this but I am not sure it is right or wrong.

public class SplashActivity extends Activity {

    // using timer to do operation at certain 3 seconds after.
    private Timer mTimer;
    private TimerTask mTimerTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();

            // execute this after 3 seconds
            mTimerTask = new TimerTask() {

                @Override
                public void run() {
                    // start the activity (Login/Home) depends on the login
                    // status
                }
            };

            mTimer = new Timer();
            mTimer.schedule(mTimerTask, 3000);

        }
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_splash,
                    container, false);
            return rootView;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        // cancel the timer if user has pressed the back button to abort it.
        if(mTimer !=null)
            mTimer.cancel();
    }

}
CRABOLO
  • 8,605
  • 39
  • 41
  • 68
N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • Personally, with the structure of your app, in the `run()` I'd replace the splash fragment with the home fragment, or alternatively you could start a brand new activity and finish this one. I think mostly it depends on how your app is built. Just as an extra note I agree with Mark that this is bad design for Android, and I would make your client aware of this. – TMH May 27 '14 at 11:02
  • @TomHart So Whatever I am trying to do writing logic of `TimerTask` in `onCreate` of the Activity is right ? – N Sharma May 27 '14 at 11:15
  • 1
    I'd say so, but I'd look at the difference between TimerTask and AsyncTask, see which once seems the best for you. – TMH May 27 '14 at 11:31

2 Answers2

7

where I should write code of timerTask and Timer to schedule a task to perform either in onCreate of the Activity or onCreateView method or something else ?

Create another Activity and write your timer task code and then navigate to your home activity.Do something like below,

public class MySplash extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash);   

        new Handler().postDelayed(new Runnable() {
        @Override
        public void run() 
        {
        startActivity(new Intent(MySplash.this,SplashActivity.class));
        finish();
        }
    }, 3000);

        }
    }

then change you home screen code like below where you need to show your fragment class only.

public class SplashActivity extends Activity {           
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();        
        }
    }

Don't forget to add the MySplash in your manifest file and to make it a launcher Activity.

Note: As per the other answer, it's not recommendable to use Splash Screen unless until it is required so much.

Reference,

http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/

Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
-8

Don't include splash screens in Android. It's bad design. It ruins user experience.

Users don't like to wait. Instead, show them your normal activity and put a ProgressBar in the ActionBar or something.

If the only reason you want a splash screen is to show your logo and brand colors, you should do that in the ActionBar. Style your ActionBar to your brand colors and put the logo of your app at the left of the ActionBar.

http://developer.android.com/design/patterns/help.html#your-app

hichris123
  • 10,145
  • 15
  • 56
  • 70
Mark Buikema
  • 2,483
  • 30
  • 53
  • 16
    Huh. but my client needs it that's why I am looking for this. Can you hint for my question ? – N Sharma May 27 '14 at 09:36
  • 1
    And if you don't have (don't want) an ActionBar? And if the graphics in you Main Activity has to be LOADED before making it appear? I think there are times where **a Splash is useful**. – Phantômaxx May 27 '14 at 11:19
  • 4
    There is nothing wrong with splash screens and the Android documentation certainly does not look down upon this practice. The article linked to in your answer refers specifically to unsolicited splash screens in reference to "Designing Help into Your App". – Matt K May 27 '14 at 21:31
  • 1
    @Williams If your client wants it, and it's bad practice, sit him down and explain why it is bad practice and what alternatives are better. – Alice May 29 '14 at 19:49
  • 7
    @Alice You live in a dream world... [clientsfromhell.net](http://clientsfromhell.net) – Danny Beckett May 30 '14 at 00:37
  • 3
    @MattK has it right. The link you provided mentions "don't use splash screens" only in the context of "don't interrupt the user's first experience of your app by providing a tutorial or instructions for use." A splash screen as a introductory title screen while assets load is perfectly fine. I would suggest, however, that the dismissal of the splash screen should in fact be based on asynchronous background asset loading and not on an explicit timer so as not to unnecessarily delay the user from starting use of the app. – Tim Sparkles May 30 '14 at 19:30
  • 1
    @DannyBeckett Most clients I've worked with are happy to address concerns from the people they hire; if they didn't hire you for your expertise, why did they hire you? Some clients are from hell, but most want to make a product that makes money, and are willing to compromise if that will achieve their goal. It's not a dream world; it's just business. – Alice May 31 '14 at 00:10
  • 1
    @Alice To the client, having their logo show when the user opens the app is "just business". – Danny Beckett May 31 '14 at 11:55
  • @DannyBeckett Show the logo of the app at the left inside the action bar. – Mark Buikema Jun 01 '14 at 15:36