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?