Skip to content

Instantly share code, notes, and snippets.

@nagakenjs
Created July 28, 2013 12:07
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nagakenjs/6098350 to your computer and use it in GitHub Desktop.
Save nagakenjs/6098350 to your computer and use it in GitHub Desktop.
Helper class for ActionBarActivity
package jp.nagakenjs.appcompathelper.app;
import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
public class ActionBarFragment extends Fragment {
private ActionBarActivity mActivity;
public ActionBarActivity getActionBarActivity() {
return mActivity;
}
@Override
public void onAttach(Activity activity) {
if (!(activity instanceof ActionBarActivity)) {
throw new IllegalStateException(getClass().getSimpleName()
+ " must be attached to a ActionBarActivity.");
}
mActivity = (ActionBarActivity) activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
mActivity = null;
super.onDetach();
}
}
package jp.nagakenjs.appcompathelper.app;
import android.app.Activity;
import android.support.v4.app.ListFragment;
import android.support.v7.app.ActionBarActivity;
public class ActionBarListFragment extends ListFragment {
private ActionBarActivity mActivity;
public ActionBarActivity getActionBarActivity() {
return mActivity;
}
@Override
public void onAttach(Activity activity) {
if (!(activity instanceof ActionBarActivity)) {
throw new IllegalStateException(getClass().getSimpleName()
+ " must be attached to a ActionBarActivity.");
}
mActivity = (ActionBarActivity) activity;
super.onAttach(activity);
}
@Override
public void onDetach() {
mActivity = null;
super.onDetach();
}
}
package jp.nagakenjs.appcompathelper.app;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
public class WebViewFragment extends ActionBarFragment {
private WebView mWebView;
private boolean mIsWebViewAvailable;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (mWebView != null) {
mWebView.destroy();
}
mWebView = new WebView(getActivity());
mIsWebViewAvailable = true;
return mWebView;
}
@Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
@Override
public void onResume() {
super.onResume();
mWebView.onResume();
}
@Override
public void onDestroyView() {
mIsWebViewAvailable = false;
super.onDestroyView();
}
@Override
public void onDestroy() {
if (mWebView != null) {
mWebView.destroy();
mWebView = null;
}
super.onDestroy();
}
public WebView getWebView() {
return mIsWebViewAvailable ? mWebView : null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment