Splash Screen in Android using Fragment

16,615

Solution 1

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

Solution 2

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/

Share:
16,615
N Sharma
Author by

N Sharma

I have done masters in Advanced Software Engineering. I have worked on various technologies like Java, Android, Design patterns. My research area during my masters is revolving around the Recommendation algorithms that E-commerce websites are using in order to recommend the products to their customers on the basis of their preferences.

Updated on June 30, 2022

Comments

  • N Sharma
    N Sharma almost 2 years

    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();
        }
    
    }
    
  • N Sharma
    N Sharma almost 10 years
    Huh. but my client needs it that's why I am looking for this. Can you hint for my question ?
  • Phantômaxx
    Phantômaxx almost 10 years
    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.
  • Matt K
    Matt K almost 10 years
    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".
  • Alice
    Alice almost 10 years
    @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.
  • Danny Beckett
    Danny Beckett almost 10 years
    @Alice You live in a dream world... clientsfromhell.net
  • Tim Sparkles
    Tim Sparkles almost 10 years
    @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.
  • Alice
    Alice almost 10 years
    @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.
  • Danny Beckett
    Danny Beckett almost 10 years
    @Alice To the client, having their logo show when the user opens the app is "just business".
  • Mark Buikema
    Mark Buikema almost 10 years
    @DannyBeckett Show the logo of the app at the left inside the action bar.