Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not pause rendering when android activity loses focus #4848

Merged
merged 11 commits into from
Apr 12, 2018
12 changes: 8 additions & 4 deletions lib/ui/window.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ enum AppLifecycleState {
/// in the foreground inactive state. Apps transition to this state when in
/// a phone call, responding to a TouchID request, when entering the app
/// switcher or the control center, or when the UIViewController hosting the
/// Flutter app is transitioning. Apps in this state should assume that they
/// may be [paused] at any time.
///
/// On Android, this state is currently unused.
/// Flutter app is transitioning.
///
/// On Android, this corresponds to an app or the Flutter host view running
/// in the foreground inactive state. Apps transition to this state when
/// another activity is focused, such as a split-screen app, a phone call,
/// a picture-in-picture app, a system dialog, or another window.
///
/// Apps in this state should assume that they may be [paused] at any time.
inactive,

/// The application is not currently visible to the user, not responding to
Expand Down
6 changes: 6 additions & 0 deletions shell/platform/android/io/flutter/app/FlutterActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ public void onBackPressed() {
super.onBackPressed();
}
}

@Override
protected void onStop() {
eventDelegate.onStop();
super.onStop();
}

@Override
protected void onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ public void onResume() {
}
}

@Override
public void onStop() {
flutterView.onStop();
}

@Override
public void onPostResume() {
if (flutterView != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ public interface FlutterActivityEvents
*/
void onDestroy();

/**
* @see android.app.Activity#onStop()
*/
void onStop();

/**
* Invoked when the activity has detected the user's press of the back key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,13 @@ public void onBackPressed() {
}
}

@Override
protected void onStop() {
eventDelegate.onStop();
super.onStop();
}


@Override
protected void onPause() {
super.onPause();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ private void attach(FlutterNativeView view) {

// Called by native to send us a platform message.
private void handlePlatformMessage(final String channel, byte[] message, final int replyId) {
assertAttached();
// The platform may not be attached immediately in certain cases where a new bundle is run -
// the native view is created in a separate thread. This mostly happens when the app restarts in dev
// mode when switching into split-screen mode. Preventing app restarts on layout and density
// changes will prevent this, and afterwards this can be changed back to an assert.
if (!isAttached()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still happen if you apply the pending engine refactoring by @chinmaygarde ? (https://github.com/chinmaygarde/flutter_engine/tree/shell)

The Dart application may fail if it sends a message to the host and the host drops the message with no response.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So looking into this more - I can reproduce the issue on master, somewhat flaky though. Might only be a debug issue

Log.d(TAG, "PlatformView is not attached");
return;
}
BinaryMessageHandler handler = mMessageHandlers.get(channel);
if (handler != null) {
try {
Expand Down
4 changes: 2 additions & 2 deletions shell/platform/android/io/flutter/view/FlutterView.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public void addActivityLifecycleListener(ActivityLifecycleListener listener) {
}

public void onPause() {
mFlutterLifecycleChannel.send("AppLifecycleState.paused");
mFlutterLifecycleChannel.send("AppLifecycleState.inactive");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth testing, I might be wrong, but I think on most Android manufacturer OSes, the lock screen might put us in this state too. We should be sure about battery usage for common cases.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll take a look at battery use. I'm fairly certain that Choreographer won't give use frames, so hopefully it looks okay.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did some research - we might be able to tell using the PowerManager API

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked a few devices (pixel 1, nexus 5, galaxy s5) - all will call onStop when exiting to the lock screen. I think this should be okay as is

}

public void onPostResume() {
Expand All @@ -278,7 +278,7 @@ public void onPostResume() {
}

public void onStop() {
mFlutterLifecycleChannel.send("AppLifecycleState.suspending");
mFlutterLifecycleChannel.send("AppLifecycleState.paused");
}

public void onMemoryPressure() {
Expand Down