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

fix(#376): fix issue of the app crashing in some phones #377

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
Expand All @@ -19,6 +21,8 @@ public class OpenSettingsDialogFragment extends Fragment {
private long lastTimeTap = 0;
private GestureHandler swipeGesture;
private static final int TIME_BETWEEN_TAPS = 500;
private View mainView;
boolean isViewSetup = false;

private final OnTouchListener onTouchListener = new OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
Expand All @@ -33,8 +37,44 @@ public boolean onTouch(View view, MotionEvent event) {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
Copy link
Contributor

Choose a reason for hiding this comment

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

So, I am probably missing something here, but in my testing, adding this line here in the onCreate was all that I needed to avoid the error in question.

In my debugging it seemed like without this line, the OpenSettingsDialogFragment.onCreate method was getting called a bunch of times when entering/exiting split-screen mode before the EmbeddedBrowserActivity.onCreate method is even called. When this happened, the wbvMain view was still null (presumably because the activity was not created yet). Eventually, the EmbeddedBrowserActivity.onCreate method is called for the new activity and that triggers the OpenSettingsDialogFragment.onCreate method to be called again (and this time the wbvMain view exists.

However, if I set setRetainInstance(true); here in OpenSettingsDialogFragment.onCreate and leave everything else as it is in master, it prevents all the extra initial calls to EmbeddedBrowserActivity.onCreate that were happening before EmbeddedBrowserActivity.onCreate and so avoids the error. When the EmbeddedBrowserActivity.onCreate function is called, that triggers a call to OpenSettingsDialogFragment.onCreate which then can access the wbvMain view at that point in the lifecycle.

I am not 100% sure about exactly what is happening here. The EmbeddedBrowserActivity.onCreate is not getting called multiple times. My theory is that there is some weird connection between the activity lifecycle and the fragment lifecycle that is causing the OpenSettingsDialogFragment.onCreate function to get called at other points during the activity lifecycle as the original EmbeddedBrowserActivity is destroyed. Setting setRetainInstance seems to decouple the fragment from the activity lifecycle so that this does not happen.

}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setupViewWithRetry();
}

private boolean shouldSetupView() {
return getActivity() != null && !isViewSetup;
}

private boolean setupView() {
View view = getActivity().findViewById(R.id.wbvMain);
view.setOnTouchListener(onTouchListener);
if (view != null) {
mainView = view;
mainView.setOnTouchListener(onTouchListener);
isViewSetup = true;
return true;
}

return false;
}

void setupViewWithRetry() {
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (!shouldSetupView() || !setupView()) {
retrySetup();
}
}

private void retrySetup() {
new Handler(Looper.getMainLooper()).postDelayed(this, 100);
}
});
}

private void countTaps(MotionEvent event) {
Expand Down
Loading
Loading