Hello.

The essence of the problem is that between SplashScreen , which is shown when you start the program, and the Activity that starts after it, there is a rather large delay.

Those. after the initial screen, the application is minimized, the phone menu is shown, and then after 2-5 seconds the Activity itself opens.

Honestly, at the very beginning everything worked fine, but after changing the code, the behavior changed, and for a couple of days I can not understand what the problem is.

The SplashActivity code SplashActivity (it is not particularly large, so I decided to bring it in full).

 public class SplashActivity extends AppCompatActivity { private DatabaseReference databaseReference; private ValueEventListener valueEventListener; private ArrayList<User> userList = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tryToConnect(); finish(); } private void tryToConnect(){ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); if (preferences.getString("Login","").equals("") || preferences.getString("Password","").equals("")){ startActivity(new Intent(this, SignInActivity.class)); finish(); } else { if (hasConnection()) { valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { userList = Globals.Downloads.getVerifiedUserList(dataSnapshot); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(SplashActivity.this); for (User user : userList) if (user.getLogin().equals(preferences.getString("Login", ""))) signIn(user); } @Override public void onCancelled(DatabaseError databaseError) { Toast.makeText(getApplicationContext(), "Ошибка в работе базы данных. Обратитесь к администратору компании или разработчику", Toast.LENGTH_LONG).show(); } }; databaseReference = FirebaseDatabase.getInstance().getReference(); databaseReference.addValueEventListener(valueEventListener); } else Toast.makeText(getApplicationContext(), "Нет подключения к интернету", Toast.LENGTH_LONG).show(); } } private void signIn(User user){ Toast.makeText(getApplicationContext(), "Вход выполнен", Toast.LENGTH_LONG).show(); if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("allowNotifications", false)) startService(new Intent(this, MessagingService.class)); Globals.currentUser = user; databaseReference.removeEventListener(valueEventListener); startActivity(new Intent(SplashActivity.this, AcceptedTicketsActivity.class)); } private boolean hasConnection() { Runtime runtime = Runtime.getRuntime(); try { Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8"); int exitValue = ipProcess.waitFor(); return (exitValue == 0); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } return false; } } 

In the manifest, it is declared as:

 <activity android:name=".SplashActivity" android:theme="@style/AppTheme.Launcher"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> 

Style:

 <style name="AppTheme.Launcher"> <item name="android:windowBackground">@drawable/background_splash</item> </style> 

I tried to chemize with the finish() method, but it did not give any results.

Thank you in advance for your response.

    2 answers 2

    In short, I tinker with it all. At the same time he corrected his mistakes in the class itself. In principle, I achieved the desired result: SplashActivity hangs right up until the opening of the next Activity .

     public class SplashActivity extends AppCompatActivity { private DatabaseReference databaseReference; private ValueEventListener valueEventListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); tryToConnect(); } @Override protected void onResume() { super.onResume(); databaseReference.addValueEventListener(valueEventListener); } @Override protected void onStop() { super.onStop(); databaseReference.removeEventListener(valueEventListener); } @Override protected void onPause() { super.onPause(); databaseReference.removeEventListener(valueEventListener); } private void tryToConnect(){ SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); if (preferences.getString("Login","").equals("") || preferences.getString("Password","").equals("")){ showSignInActivity(); } else { if (hasConnection()) { valueEventListener = new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { ArrayList<User> userList = Globals.Downloads.getVerifiedUserList(dataSnapshot); SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(SplashActivity.this); checkVerificationData(userList, preferences.getString("Login", ""), preferences.getString("Password", "")); } @Override public void onCancelled(DatabaseError databaseError) { Toast.makeText(getApplicationContext(), "Ошибка в работе базы данных. Обратитесь к администратору компании или разработчику", Toast.LENGTH_LONG).show(); SplashActivity.this.finish(); } }; databaseReference = FirebaseDatabase.getInstance().getReference(); } else { Toast.makeText(getApplicationContext(), "Нет подключения к интернету", Toast.LENGTH_LONG).show(); showSignInActivity(); } } } private void showSignInActivity(){ startActivity(new Intent(SplashActivity.this, SignInActivity.class)); SplashActivity.this.finish(); } private void signIn(User user){ Toast.makeText(getApplicationContext(), "Вход выполнен", Toast.LENGTH_LONG).show(); if (PreferenceManager.getDefaultSharedPreferences(this).getBoolean("allowNotifications", false)) startService(new Intent(this, MessagingService.class)); Globals.currentUser = user; startActivity(new Intent(SplashActivity.this, AcceptedTicketsActivity.class)); SplashActivity.this.finish(); } private void checkVerificationData(ArrayList<User> userList, String login, String password) { int i = 0; while (!login.equals(userList.get(i).getLogin()) && ++i < userList.size()); if (i >= userList.size()) { Toast.makeText(getApplicationContext(), "Логин и/или пароль введен неверно. Повторите попытку", Toast.LENGTH_LONG).show(); showSignInActivity(); } else if (login.equals(userList.get(i).getLogin()) && password.equals(userList.get(i).getPassword())) signIn(userList.get(i)); else { Toast.makeText(getApplicationContext(), "Логин и/или пароль введен неверно. Повторите попытку", Toast.LENGTH_LONG).show(); showSignInActivity(); } } private boolean hasConnection() { ConnectivityManager cm = (ConnectivityManager)SplashActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo activeNetwork = cm.getActiveNetworkInfo(); return activeNetwork.isConnectedOrConnecting(); } } 

    Style changed to this (forgot parent to specify)

     <style name="AppTheme.Launcher" parent="AppTheme.NoActionBar"> <item name="android:windowBackground">@drawable/background_splash</item> </style> <style name="AppTheme.NoActionBar" parent="AppTheme"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> 

      It seems to me that checking in such a brutal way the connection with the Internet is not very good. And the ipProcess.waitFor() spell does not belong in the main thread. Try searching for a more advanced connection test practice, start here.

      • Perhaps, of course, this is one of the reasons for this behavior of SplashActivity , but even after replacing the connectivity test with what is presented in the documentation, the delay in opening the next Activity is still present. I think there is something else that I lose sight of. - ahgpoug