java - Android display Splash-Screen while loading -


i have android app, shows "splash screen" 3 seconds. after that, mainactivity gets loaded.

unfortunately mainactivity takes additional ~4 seconds load. on first startup longer. when app loaded, runs smooth.

now how can achieve it, mainactivity gets loaded, during display of splash screen? should display image until whole thing loaded completely. have read async-task, i'm not sure put , how use properly. can me please?

splashscreen.java

public class splashscreen extends activity {     private static int splash_time_out = 3000;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_startup);          new handler().postdelayed(new runnable() {             @override             public void run() {                 intent = new intent(splashscreen.this, mainactivity.class);                 startactivity(i);                 finish();             }         }, splash_time_out);     } } 

mainactivity.java

public class mainactivity extends activity implements onclicklistener, mediacontroller.mediaplayercontrol {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          //some heavy processing         //starting services         //starting google text speech         //and on...      }  } 

if there no specific constraints time splash screen should shown, use asynctask in following way:

public class splashscreen extends activity {     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_startup);         startheavyprocessing();      }      private void startheavyprocessing(){        new longoperation().execute("");     }      private class longoperation extends asynctask<string, void, string> {          @override         protected string doinbackground(string... params) {             //some heavy processing resulting in data string             (int = 0; < 5; i++) {                 try {                     thread.sleep(1000);                 } catch (interruptedexception e) {                     thread.interrupted();                 }             }             return "whatever result have";         }          @override         protected void onpostexecute(string result) {             intent = new intent(splashscreen.this, mainactivity.class);             i.putextra("data", result);             startactivity(i);             finish();         }          @override         protected void onpreexecute() {}          @override         protected void onprogressupdate(void... values) {}     } } 

if resulting data if of nature string put parcelable object activity. in oncreate can retrieve data with:

getintent().getextras.getstring('data');


Comments