Reputation: 308
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.
Is the testing device slow? I don't think that's the case.
I've tested in Android 10 VM (1 Second delay),
Android 9, 8, 7, 6, 5.1, 5 VM (1 Second delay),
Nokia 1 Plus (Slow) With Android 10 and 9 (1/2 Second delay),
Nokia 5.1 With Android 9 (1/4 Second delay),
Galaxy s6 Edge with Lineage 16 (Android 9) and Android 7.0 stock No Delay,
Galaxy s5 Lineage 15 (Android 8), Lineage 14 (Android 7), Stock 6.0 (1/8 Second delay),
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>
Upvotes: 5
Views: 2546
Reputation: 308
After Much research , i conclude my answer for my own question as follows
the cause of this is not due to any dependency or code optimisation. the reason that it works faster on higher-ed phones can be assumed that this is due to the obvious higher performance of the device itself. but this is not a satisfying answer as this is code posted above is as minimalistic as possible for an android app. it just opens an empty activity.
The reason behind this is that most apps (i'd like to say all) on the Android operating system run on a Java virtual machine.
and you know what that means. it means is not giving native level performance and causes a lot of overhead entropy. this is how Java is and why it is cross platform, also one of the main reason it became popular.
There is NO Solution to this. its not within our control.
id like to see android moving a way from Java if thats even possibility. also one of the reason why android phones nowadays require more RAM. and iOS needing less ram and to keep apps background.
Let me know you thoughts on this
Upvotes: 0
Reputation: 121
You can try the android profiler to trace/investigate this more. But from an overall perspective, there should not be any delay for launching an activity with a simple layout. Other things that could impact the delay is the device resource availability. So, if your code is the same as you mentioned in your question, it must be device performance which is creating the delay.
Upvotes: 2