1

When I run the app on my physical device, it launches well but hangs on the splash screen. When I use the emulator(Pixel XL API 29) ,it launches the splash screen then get stuck on splash screen. What could be the problem?

My splash Activity

package com.toto.vpn.Activities;

import android.content.Intent;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.toto.vpn.R;

public class SplashScreen extends AppCompatActivity {
    CoordinatorLayout coordinatorLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {


        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference typeRef = database.getReference("type");
        DatabaseReference indratech_toto_27640849_admob_id = database.getReference("indratech_toto_27640849_admob_id");
        DatabaseReference indratech_toto_27640849_ad_banner = database.getReference("indratech_toto_27640849_ad_banner");
        DatabaseReference indratech_toto_27640849_aad_native = database.getReference("indratech_toto_27640849_aad_native");
        DatabaseReference indratech_toto_27640849_fb_native = database.getReference("indratech_toto_27640849_fb_native");
        DatabaseReference indratech_toto_27640849_fb_interstitial = database.getReference("indratech_toto_27640849_fb_interstitial");
        DatabaseReference indratech_toto_27640849_ad_interstitial = database.getReference("indratech_toto_27640849_ad_interstitial");
        DatabaseReference indratech_toto_27640849_admob_reward = database.getReference("indratech_toto_27640849_admob_reward");
        DatabaseReference indratech_toto_27640849_fb_reward = database.getReference("indratech_toto_27640849_fb_reward");
        DatabaseReference copyright_indratech_official_dont_change_the_value = database.getReference("copyright_indratech_official_dont_change_the_value");

        String TAG = "Firebase";

        typeRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                MainActivity.type = value;
                Log.d(TAG,"Type"+value);
                Log.d(TAG,"Type"+MainActivity.type);

            }

            @Override
            public void onCancelled(DatabaseError error) {

                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        indratech_toto_27640849_aad_native.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                MainActivity.admob_native_id = value;
                Log.d(TAG,"Native"+value);
                Log.d(TAG,"Native"+MainActivity.admob_native_id);

            }

            @Override
            public void onCancelled(DatabaseError error) {

                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });


        indratech_toto_27640849_admob_id.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                MainActivity.indratech_toto_27640849_admob_id = value;
                Log.d(TAG,"Admob ID"+value);
                Log.d(TAG,"Admob ID"+MainActivity.indratech_toto_27640849_admob_id);
                try {
                    ApplicationInfo applicationInfo = getPackageManager().getApplicationInfo(getPackageName(),PackageManager.GET_META_DATA);
                    Bundle bundle = applicationInfo.metaData;
                    applicationInfo.metaData.putString("com.google.android.gms.ads.APPLICATION_ID",MainActivity.indratech_toto_27640849_admob_id);
                    String apiKey = bundle.getString("com.google.android.gms.ads.APPLICATION_ID");
                    Log.d(TAG,"The saved id is "+MainActivity.indratech_toto_27640849_admob_id);
                    Log.d(TAG,"The saved id is "+apiKey);
                } catch (PackageManager.NameNotFoundException e) {
                    e.printStackTrace();
                }catch (NullPointerException e){
                    e.printStackTrace();
                }

            }

            @Override
            public void onCancelled(DatabaseError error) {

                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });


        indratech_toto_27640849_ad_banner.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                MainActivity.admob_banner_id = value;
                Log.d(TAG,"Admob Banner"+value);
                Log.d(TAG,"Admob Banner"+MainActivity.admob_banner_id);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        // Read from the database
        indratech_toto_27640849_ad_interstitial.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                MainActivity.admob_interstitial_id = value;
                Log.d(TAG,"Admob interstitial"+value);
                Log.d(TAG,"Admob interstitial"+MainActivity.admob_interstitial_id);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        // Read from the database
        indratech_toto_27640849_fb_native.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                MainActivity.indratech_toto_27640849_fb_native_id = value;
                Log.d(TAG,"indratech_toto_27640849_fb_native"+value);
                Log.d(TAG,"indratech_toto_27640849_fb_native"+MainActivity.indratech_toto_27640849_fb_native_id);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        // Read from the database
        indratech_toto_27640849_fb_interstitial.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                MainActivity.indratech_toto_27640849_fb_interstitial_id = value;
                Log.d(TAG,"indratech_toto_27640849_fb_interstitial"+value);
                Log.d(TAG,"indratech_toto_27640849_fb_interstitial"+MainActivity.indratech_toto_27640849_fb_interstitial_id);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        // Read from the database
        indratech_toto_27640849_fb_reward.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                MainActivity.indratech_toto_27640849_fb_reward_id = value;
                Log.d(TAG,"indratech_toto_27640849_fb_reward"+value);
                Log.d(TAG,"indratech_toto_27640849_fb_reward"+MainActivity.indratech_toto_27640849_fb_reward_id);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        // Read from the database
        indratech_toto_27640849_admob_reward.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                MainActivity.indratech_toto_27640849_admob_reward = value;
                Log.d(TAG,"indratech_toto_27640849_admob_reward"+value);
                Log.d(TAG,"indratech_toto_27640849_admob_reward"+MainActivity.indratech_toto_27640849_admob_reward);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });

        copyright_indratech_official_dont_change_the_value.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                String value = dataSnapshot.getValue(String.class);
                MainActivity.copyright_indratech_official_dont_change_the_value = value;
                Log.d(TAG,"copyright_indratech_official_dont_change_the_value"+value);
                Log.d(TAG,"copyright_indratech_official_dont_change_the_value"+MainActivity.copyright_indratech_official_dont_change_the_value);

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen_indratech);
        coordinatorLayout = findViewById(R.id.cordi);
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if (!Utility.isOnline(getApplicationContext())) {


            Snackbar snackbar = Snackbar
                    .make(coordinatorLayout, "Check internet connection", Snackbar.LENGTH_LONG);
            snackbar.show();


        } 

    }
}

My Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.toto.vpn">



    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.WRITE_SYNC_SETTINGS" />
    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

    <application
        android:name="com.toto.vpn.App"
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:networkSecurityConfig="@xml/network_security_config"
        tools:replace="android:networkSecurityConfig"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name="com.toto.vpn.Activities.Apps_Picker"></activity>
        <activity android:name="com.toto.vpn.Activities.Black_Battery_Saver" />
        <activity android:name="com.toto.vpn.Activities.Saving_Power_Comp" />
        <activity android:name="com.toto.vpn.Activities.UApplying" />
        <activity android:name="com.toto.vpn.Activities.NormalMode" />
        <activity android:name="com.toto.vpn.Activities.UPopUp" />
        <activity android:name="com.toto.vpn.Activities.PopUp_SavingPower" />
        <activity android:name="com.toto.vpn.Activities.BatteryActivity" />
        <activity android:name="com.toto.vpn.Activities.ScannerCPU" />
        <activity android:name="com.toto.vpn.Activities.CPUCoolerActivity" />
        <activity android:name="com.toto.vpn.Activities.SpeedBoosterActivity" />
        <activity android:name="com.toto.vpn.Activities.UnlockAllActivity" />

        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-1895176674912401~3090708404" />

        <activity
            android:name="com.toto.vpn.Activities.IntroActivity"
            android:theme="@style/AppTheme1" />
        <activity
            android:name="com.toto.vpn.Activities.SplashScreen"
            android:theme="@style/Splashscreentheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.toto.vpn.Activities.Servers"
            android:parentActivityName="com.toto.vpn.Activities.MainActivity" />
        <activity android:name="com.toto.vpn.Activities.MainActivity" />
        <activity android:name="com.facebook.ads.AudienceNetworkActivity" android:hardwareAccelerated="true" />
    </application>

</manifest>

Thanks for your help and I really appretiate you helping me, because I've been dealing with these for more than 5 days now. Thank You

MaxMan MX
  • 11
  • 1
  • 3
  • Can you also add the code where you're launching the activity via intent? Currently, I don't see such a call in the current source code. – Nikhil Jain Mar 27 '22 at 14:24

1 Answers1

0

As per the codebase, you are performing Thread.sleep(3000); operation in your #onCreate function. This makes the UI freeze for 3seconds. Hence the UI is frozen. Also, you are initiating way too many objects (Database references) in your #onCreate, which again slow down the thread. Combining all the above, it makes your UI thread buzy an slow making UI freeze.

Explaination

  • Android can have multiple thread. But it can only have one UI thread (a.k.a. main thread). The operations performed in Activity's lifecycle callbacks (like #onCreate, #onPause) are executed on this UI thread only.
  • Also, any UI operations such as button click, scroll, swipe gestures are handled by this UI thread only.
  • Now, due to Thread.sleep or other heavy operation, the UI thread cannot handle any other operations (such as user click/scroll), which makes the screen non-responsive.

How to fix this

  • Get rid of Thread.sleep
    • In case this call was added for testing/debugging, try to remove the Thread.sleep call.
    • In case you need this call to wait for 3 seconds before making further checks, try to use #postDelayed function (Ref: How to use postDelayed() correctly in Android Studio? ) or use Kotlin coroutines.
  • Reduce object creation in #onCreate
    • Try to reduce the object reference creation by moving that code in #onResume.
    • This will make sure that the UI is created and interactable to the user. This way user will have some UI to look at while you are fetching some data.
    • If you can, put all the references and data fetching into another thread, this will reduce burden on main thread and will make your app more responsive.
Ashok
  • 2,411
  • 1
  • 13
  • 23