I have a simple activity without UI. I wanted to check the lifecycle methods of an activity during call.
When a notification for call arrives, nothing happens as expected. When I accept the call, then the activity for call is covering up on my activity. So, ideally onStop()
should be called immediately. I have checked the logs and only onPause()
is being called while accepting the call. But after 2-3 seconds onStop()
is also being called.
Activity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart: ");
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "onStop: ");
}
@Override
protected void onPause() {
super.onPause();
Log.e(TAG, "onPause: ");
}
@Override
protected void onResume() {
super.onResume();
Log.e(TAG, "onResume: ");
}
}
Manifest
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Logs
05-17 22:10:25.025 E/MainActivity: onStart:
05-17 22:10:25.054 E/MainActivity: onResume:
When call has been accepted:
05-17 22:10:34.405 E/MainActivity: onPause:
After 2-4 seconds:
05-17 22:10:38.144 E/MainActivity: onStop:
As per the documentation of onStop()
:
Called when you are no longer visible to the user. You will next receive either onRestart(), onDestroy(), or nothing, depending on later user activity.
Here, the other activity is covering my activity and hiding it. So, onStop()
should be called instantly after onPause()
.
I have tested it on Moto G4 device and Nexus 5 emulator. Both are showing same behavior. What is the reason of delayed onStop() call?
Can anyone explain the internal details?