Challenge: Using the back button for browser history


#1

Here’s my solution to the challenge.

In PhotoPageFragment.java I have the following code:

public boolean webViewGoBack() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return true;
} else {
return false;
}
}

In PhotoPageActivity.java I have the following code:

@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_container);
if (!((PhotoPageFragment) fragment).webViewGoBack()) {
super.onBackPressed();
}
}

In fragement_photo_page.xml I have added the following:

android:id="@+id/fragment_container"

Cheers
Wayne


#2

If you meant by “fragment_container” FrameLayout (which was in initial activity) then there are sometimes null pointer exceptions