1

can someone explain me how to make splash screen in fragment using android studio. I want to make the HomeFragment will appear after the splash. As your information, I am using navigation drawer in my apps.

Hilda
  • 23
  • 2

3 Answers3

0

Do not try to create splash screen in fragment. Create your splash screen as launcher activity, put some timer on it then load your main activity that extends FragmentActivity:

public class SplashScreen extends Activity {

    // Splash screen timer
    private static int SPLASH_TIME_OUT = 3000;

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

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
                Intent i = new Intent(SplashScreen.this, MainActivity.class);
                startActivity(i);

                // close this activity
                finish();
            }
        }, SPLASH_TIME_OUT);
    }     
}

Your MainActivity will serve as fragment container. In that activity load your HomeFragment initially.

Pang
  • 9,564
  • 146
  • 81
  • 122
Tomislav
  • 3,181
  • 17
  • 20
0

Make Splashscreen as Activity. Here is similar problem already resolved: How do I make a splash screen?

Community
  • 1
  • 1
Damian Kozlak
  • 7,065
  • 10
  • 45
  • 51
0
@Override
    public void onViewCreated(@NonNull final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated( view, savedInstanceState );
        Thread thread=new Thread(  ) {
            @Override
            public void run() {
                try {
                    sleep( 3000 );
                    Navigation.findNavController(view).navigate( R.id.action_splashScreen_to_mainFragment );
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        };
        thread.start();
    }