3

When you first start an app, the app has to download quite a bit of data from server, like locations, criteria information and so on. I wanted to show the splashscreen as efficient as I could, but even if i don Async Task it just doesn't work fast. You are on splash screen for 30s, only at first run then it is less than 1.5 s. My question is... How can I start background work and move to next activity, without holding for 30s at SplashActivity

public class ActivitySplash extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //    if (MyFunctionsRealm.differsTheLocationsNumberInRealm()) {  //Če najdeš boljšo rešitev super :)
    startAsyncTaskJsonCodeRegisters();
    //  }

    Realm realm = Realm.getDefaultInstance();
    RealmUser_Personal user = MyFunctionsRealm.getLoggedInUser();
    if (user != null) {
        API_Network_Service.checkUpdate(user.getId(), "user", user);
    }
    realm.close();
    // MyFunctions.goToActivityAfter(1500, this, ActivityMain.class);
    MyFunctions.fromActivityToActivity(this, ActivityMain.class);
}

private void startAsyncTaskJsonCodeRegisters() {
    new API_Network_Service.getLocations_Criteria(getApplicationContext()).execute();
}

}

Get location criteria function is quite large, but basicly i download text from url, parse it like json, then just process that json file, and save it to Realm database.

This is how an example how i save something that is new.

  Realm realm = Realm.getDefaultInstance();
        final SloveniaLocation location= realm.where(SloveniaLocation.class).equalTo("id", id).equalTo("name", ime).equalTo("parent_id", parent_id).findFirst();
        realm.close();
        if (location == null) {
            new Async_Save_Location(id, ime, position, parent_id).execute();
        }

Async_Save_Location is nothing special, just this and constructor,getter and setter:

    @Override
protected Void doInBackground(Void... params) {
    Realm realm = Realm.getDefaultInstance();
    realm.executeTransaction(new Realm.Transaction() {
        @Override
        public void execute(Realm realm) {
            SloveniaLocation nova_lokacija = realm.createObject(SloveniaLocation.class);
            nova_lokacija.setId(id);
            nova_lokacija.setIme(ime);
            nova_lokacija.setParent_id(parent_id);
            nova_lokacija.setPosition(position);
        }
    });
    realm.close();

    return null;
}

For SplashScreen i'm using a theme, so it doesn't show black screen:

<style name="ThemeSplash" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowBackground">@drawable/drawable_splashcreen</item>
</style>

Any ideas?

Christian Melchior
  • 19,978
  • 5
  • 62
  • 53
WinterChilly
  • 1,549
  • 3
  • 21
  • 34
  • 1
    Check here -- http://stackoverflow.com/questions/37296544/android-takes-more-time-on-application-start-up-during-first-time-launch -- All you need to do is uncheck that box (Enable Instant Run ..... ) make sure you add ( multiDexEnabled true) in gradle ---> defaultConfig -- Othewise you get an error --- you find your Application at FIRST RUN now runs 100 times faster. --- OR -- create an unsigned APK instead of clicking the Play button in AS and install that instead on the Device – Tasos Aug 21 '16 at 02:04
  • 1
    To clarify why you need (multiDexEnabled true) after you uncheck that setting read here https://developer.android.com/studio/build/multidex.html -- so you may or may not get an error so you may not need it in the Gradle. If you do get an error when building the App and alternative would be to set the (minifyEnabled true) which Removes unused code with ProGuard. That may do the trick other wise use (multiDexEnabled true). I just tried on my App and (multiDexEnabled true) worked (minifyEnabled true) didnt. – Tasos Aug 21 '16 at 02:22
  • By the way, this is a good guide on Splash Screens and how to do them properly https://www.bignerdranch.com/blog/splash-screens-the-right-way/ – Tasos Aug 21 '16 at 02:29
  • @Tasos Instant run is for gradle build, and yes that is faster, and it doesn't help with the app if you send your app to your friend, and let them test the app. The app will still need some time for the first load. Not in the Android Studio but in the actuall App. To reply to your third comment, this is already implemented ;) – WinterChilly Aug 21 '16 at 08:19
  • 1
    Give it a Test. Build an APK for your APP and install that on a Real Device. Does it run as fast for FIRST RUN as it does unchecking that Option??? Let me know – Tasos Aug 21 '16 at 13:49
  • I will as soon as i can :), then i'll let you know. – WinterChilly Aug 21 '16 at 18:08
  • Ok i didn't expect that... it worked... do you know the reason why this is faster? – WinterChilly Aug 21 '16 at 21:04
  • In my 1st comment link the person that has 12 points reported "This issue happens only in debug mode" with the current AS but ok when you build an APK – Tasos Aug 21 '16 at 23:48

0 Answers0