Commit 435c1b51 authored by Zhiheng Vincent Li's avatar Zhiheng Vincent Li Committed by Commit Bot

Change fragment's onCreateView() and onStart() to make it resumable

Fragment is detached from the activity when user says "OKG, set a timer for 1 minute", or use swipe from edge to "close" the player. When it is attached back to activity by clicking on home view's tile, onCreateView() and onStart() will be called again, but cast views and CastWebContentsSurfaceHelper already attached with current WebContents should be reused.

Change-Id: Ibc6dc3a31cd0ff3fc31e16c879537db43185b2f4

Bug: b/72838507

Test: 1. Say "OKG, play a video" and see cast fragment shown; 2. Say "OKG, set a timer for 1 minute" and dismiss the timer page, see cast fragment recovered correctly and video could be resume; 3. Check log output and see 2nd call of onCreateView() and onStart() won't recreate new fragment view and reset webconent.

TODO(vincentli): Add a test case to cover CastWebContentsFragment's lifecycle

Change-Id: Ibc6dc3a31cd0ff3fc31e16c879537db43185b2f4
Reviewed-on: https://chromium-review.googlesource.com/898602
Commit-Queue: Zhiheng(Vincent) Li <vincentli@google.com>
Reviewed-by: default avatarStephen Lanham <slan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#534131}
parent 147cb419
...@@ -26,6 +26,8 @@ import org.chromium.content_public.browser.WebContents; ...@@ -26,6 +26,8 @@ import org.chromium.content_public.browser.WebContents;
* CastWebContentsFragment should be removed from the activity holding it. * CastWebContentsFragment should be removed from the activity holding it.
* Similarily, if the fragment is removed from a activity or the activity holding * Similarily, if the fragment is removed from a activity or the activity holding
* it is destroyed, CastContentWindowAndroid should be notified by intent. * it is destroyed, CastContentWindowAndroid should be notified by intent.
*
* TODO(vincentli): Add a test case to test its lifecycle
*/ */
public class CastWebContentsFragment extends Fragment { public class CastWebContentsFragment extends Fragment {
private static final String TAG = "cr_CastWebContentFrg"; private static final String TAG = "cr_CastWebContentFrg";
...@@ -34,6 +36,8 @@ public class CastWebContentsFragment extends Fragment { ...@@ -34,6 +36,8 @@ public class CastWebContentsFragment extends Fragment {
private CastWebContentsSurfaceHelper mSurfaceHelper; private CastWebContentsSurfaceHelper mSurfaceHelper;
private View mFragmentRootView;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate"); Log.d(TAG, "onCreate");
...@@ -52,11 +56,14 @@ public class CastWebContentsFragment extends Fragment { ...@@ -52,11 +56,14 @@ public class CastWebContentsFragment extends Fragment {
.show(); .show();
return null; return null;
} }
if (mFragmentRootView == null) {
return inflater.cloneInContext(getContext()) mFragmentRootView = inflater.cloneInContext(getContext())
.inflate(R.layout.cast_web_contents_activity, null); .inflate(R.layout.cast_web_contents_activity, null);
}
return mFragmentRootView;
} }
@Override @Override
public Context getContext() { public Context getContext() {
if (mPackageContext == null) { if (mPackageContext == null) {
...@@ -69,11 +76,12 @@ public class CastWebContentsFragment extends Fragment { ...@@ -69,11 +76,12 @@ public class CastWebContentsFragment extends Fragment {
public void onStart() { public void onStart() {
Log.d(TAG, "onStart"); Log.d(TAG, "onStart");
super.onStart(); super.onStart();
if (mSurfaceHelper == null) { if (mSurfaceHelper != null) {
mSurfaceHelper = new CastWebContentsSurfaceHelper(getActivity(), /* hostActivity */ return;
}
mSurfaceHelper = new CastWebContentsSurfaceHelper(getActivity(), /* hostActivity */
(FrameLayout) getView().findViewById(R.id.web_contents_container), (FrameLayout) getView().findViewById(R.id.web_contents_container),
true /* showInFragment */); true /* showInFragment */);
}
Bundle bundle = getArguments(); Bundle bundle = getArguments();
bundle.setClassLoader(WebContents.class.getClassLoader()); bundle.setClassLoader(WebContents.class.getClassLoader());
String uriString = bundle.getString(CastWebContentsComponent.INTENT_EXTRA_URI); String uriString = bundle.getString(CastWebContentsComponent.INTENT_EXTRA_URI);
...@@ -93,7 +101,6 @@ public class CastWebContentsFragment extends Fragment { ...@@ -93,7 +101,6 @@ public class CastWebContentsFragment extends Fragment {
public void onPause() { public void onPause() {
Log.d(TAG, "onPause"); Log.d(TAG, "onPause");
super.onPause(); super.onPause();
if (mSurfaceHelper != null) { if (mSurfaceHelper != null) {
mSurfaceHelper.onPause(); mSurfaceHelper.onPause();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment