How to preload MainActivity into SplashActivity so that there is no delay when MainActivity starts?

I have a working surge

public class MainSplash extends AppCompatActivity { private final int SPLASH_DISPLAY_LENGTH = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); new Handler().postDelayed(new Runnable() { @Override public void run() { Intent mainIntent = new Intent(MainSplash.this, MainMenu.class); MainSplash.this.startActivity(mainIntent); MainSplash.this.finish(); } }, SPLASH_DISPLAY_LENGTH); } } 

and I want to add some buttons like share , more , etc.

and wen I do this by removing the handler and SPLASH_DISPLAY_LENGTH , adding buttons to xml and processing clicks on them just like any other actions, and set the Start button to launch MainActivity , t25> starts after a few seconds (after 1, 2 seconds of loading )

But I want to handle MainActivity load MainActivity in SplashActivity ,

How can i do this?

Here is an example of SplashActivity after adding buttons

 public class MainSplash extends AppCompatActivity implements View.OnClickListener { //private final int SPLASH_DISPLAY_LENGTH = 1000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.splash); Button btn1 = (Button) findViewById(R.id.button); Button btn2 = (Button) findViewById(R.id.button2); Button btn3 = (Button) findViewById(R.id.button3); Button btn4 = (Button) findViewById(R.id.button4); btn1.setOnClickListener(this); btn2.setOnClickListener(this); btn3.setOnClickListener(this); btn4.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button4: Intent mainIntent = new Intent(this,MainMenu.class); startActivity(mainIntent); finish(); break; } } } 
-3
java android splash-screen
Jan 28 '16 at 7:07
source share
2 answers

you can try adding a delay while loading MainActivity by putting a handler inside onCLick

0
Jan 28 '16 at 7:20
source share

Well, if someone else has the same problem as when loading Mainactivity from SplashActivity , we get a black screen for a second or two, this is the loading time for Mainactivity , and it really does not look good.

To remove it, I just added Fragment for the splash screen instead of SplashActivity and another fragment for the main interface.

This Activity method is loaded when the application starts like this, and the transaction from the splash fragment to the main fragment does not take time :)

Hope this helps someone.

0
Apr 16 '16 at 8:40
source share



All Articles