Android splash screen using slice

I am developing an Android application where I need to add a splash screen for my application. I usually used only Activityuntil now, but for this project ADT Fragmentalso creates with Activity.

Now I have a confusion when I have to write code timerTaskand Timerto schedule a task to be executed in a onCreatemethod Activityor onCreateViewsomething else?

Currently I have written like this, but I'm not sure if this 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();
    }

}
+4
source share
2 answers

Do not include screensavers in Android. This is a bad design. This destroys the user interface.

. ProgressBar ActionBar - .

, , - , ActionBar. ActionBar ActionBar.

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

-9

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 ?

Activity , . - ,

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);

        }
    }

, , .

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();        
        }
    }

MySplash Activity.

. Splash Screen, .

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

+6

Source: https://habr.com/ru/post/1542149/


All Articles