Search code examples
javaandroidandroid-activityandroid-lifecycle

Weird behavior of activity life cycle during incoming call : delayed onStop()


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?

Screenshots call is running


Solution

  • Based on the documentation:

    After receiving onPause() call you will usually receive a following call to onStop() (after the next activity has been resumed and displayed), however in some cases there will be a direct call back to onResume() without going through the stopped state.

    Although the documentation for onStop() is a bit perplexing, which claims to call it as soon as Activity is no more visible but there is a slight delay before it gets called (it depends on next activity being displayed).

    In case where you receive a call, there is a trivial delay caused after call enters foreground. I have observed this delay on my personal phone too. This delay will cause the onStop() call to be delayed.