Saturday, 19 June 2021

How to reduce or remove the delay on starting an android activity from button click

I've noticed this issue for a long time. when I open an activity through a button, I can feel a small delay before the activity "STARTS" to launch. like 1 second. even though both activities are empty. as if it's delaying on purpose. every other stackoverflow thread I checked regarding this has some kind of load which slows it down. but this is plain empty, No-load or background tasks.

what I mean by Delay : when I click the button the app does nothing for like a second then starts the transition on launching the next activity. the user might think the app is unresponsive.

Testing Device is slow? i don't think that's the case,

  1. I've tested in Android 10 VM (1 Second delay),

  2. Android 9,8,7,6,5.1,5 VM (1 Second delay),

  3. Nokia 1 Plus (Slow) With Android 10 and 9 (1/2 Second delay),

  4. Nokia 5.1 With Android 9 (1/4 Second delay),

  5. Galaxy s6 Edge with Lineage 16 (Android 9) and Android 7.0 stock No Delay,

  6. Galaxy s5 Lineage 15 (Android 8), Lineage 14 (Android 7), Stock 6.0 (1/8 Second delay),

  7. Galaxy m31 and m21 and A51 with Android 9 One UI1? (1/2 Second delay),

I do see a pattern where faster phones have less delay, regardless of the result, other apps work more responsively in the same phones. google apps on the same devices work much more responsively

Android Studio 4.0

Any idea? of the cause?

MainActivity Code:

public class MainActivity extends AppCompatActivity {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
            }
            
            public void Onclick(View view){
                startActivity(new Intent(this,MainActivity2.class));
            }
        }

MainActivity activity_main:

<?xml version="1.0" encoding="utf-8"?>
            <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context=".MainActivity">

                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:onClick="Onclick"
                    android:text="Open Activity 2"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />
      </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity2

public class MainActivity2 extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main2);
        }
    }

MainActivity2 activity_main2:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity2">

    </androidx.constraintlayout.widget.ConstraintLayout>


from How to reduce or remove the delay on starting an android activity from button click

No comments:

Post a Comment