Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
The Harder Way: WebView

Listing 30.5  Setting up your web browser fragment (PhotoPageFragment.java)


public class PhotoPageFragment extends VisibleFragment {
private static final String ARG_URI = "photo_page_url";


private Uri mUri;
private WebView mWebView;


public static PhotoPageFragment newInstance(Uri uri) {
Bundle args = new Bundle();
args.putParcelable(ARG_URI, uri);


PhotoPageFragment fragment = new PhotoPageFragment();
fragment.setArguments(args);
return fragment;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


mUri = getArguments().getParcelable(ARG_URI);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_photo_page, container, false);


mWebView = (WebView) v.findViewById(R.id.web_view);


return v;
}
}


For now, this is just a skeleton. You will fill it out a bit more in a moment. But first, create the
containing PhotoPageActivity class using good old SingleFragmentActivity.


Listing 30.6  Creating web activity (PhotoPageActivity.java)


public class PhotoPageActivity extends SingleFragmentActivity {


public static Intent newIntent(Context context, Uri photoPageUri) {
Intent i = new Intent(context, PhotoPageActivity.class);
i.setData(photoPageUri);
return i;
}


@Override
protected Fragment createFragment() {
return PhotoPageFragment.newInstance(getIntent().getData());
}
}

Free download pdf