Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jan 23rd, 2013  |  syntax: None  |  size: 2.91 KB  |  views: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public class FragmentStackSupport extends SherlockFragmentActivity {
  2. int mStackLevel = 1;
  3.  
  4. public static int THEME = R.style.Theme_Sherlock_Light;
  5.  
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8.     setTheme(THEME); //Used for theme switching in samples
  9.     super.onCreate(savedInstanceState);
  10.     setContentView(R.layout.fragment_stack);
  11.  
  12.  
  13.     if (savedInstanceState == null) {
  14.         // Do first time initialization -- add initial fragment.
  15.         Fragment newFragment = Web.newInstance(mStackLevel);
  16.         FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  17.         ft.add(R.id.simple_fragment, newFragment).commit();
  18.     } else {
  19.         mStackLevel = savedInstanceState.getInt("level");
  20.     }
  21. }
  22.  
  23. @Override
  24. public void onSaveInstanceState(Bundle outState) {
  25.     super.onSaveInstanceState(outState);
  26.     outState.putInt("level", mStackLevel);
  27. }
  28.  
  29.  
  30. public void addFragmentToStack() {
  31.     mStackLevel++;
  32.  
  33.     // Instantiate a new fragment.
  34.     Fragment newFragment = Web.newInstance(mStackLevel);
  35.  
  36.     // Add the fragment to the activity, pushing this transaction
  37.     // on to the back stack.
  38.     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  39.     ft.replace(R.id.simple_fragment, newFragment);
  40.     ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
  41.     ft.addToBackStack(null);
  42.     ft.commit();
  43. }
  44.  
  45.  
  46.  
  47. public static class Web extends SherlockFragment {
  48.     int mNum;
  49.  
  50.     /**
  51.      * Create a new instance of CountingFragment, providing "num"
  52.      * as an argument.
  53.      */
  54.     static Web newInstance(int num) {
  55.         Web f = new Web();
  56.  
  57.         // Supply num input as an argument.
  58.         Bundle args = new Bundle();
  59.         args.putInt("num", num);
  60.         f.setArguments(args);
  61.  
  62.         return f;
  63.     }
  64.  
  65.     /**
  66.      * When creating, retrieve this instance's number from its arguments.
  67.      */
  68.     @Override
  69.     public void onCreate(Bundle savedInstanceState) {
  70.         super.onCreate(savedInstanceState);
  71.         mNum = getArguments() != null ? getArguments().getInt("num") : 1;
  72.     }
  73.  
  74.     /**
  75.      * The Fragment's UI is just a simple text view showing its
  76.      * instance number.
  77.      */
  78.     @Override
  79.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  80.                              Bundle savedInstanceState) {
  81.         View v = inflater.inflate(R.layout.hello_world, container, false);
  82.         View wv = v.findViewById(R.id.webview);
  83.         WebView webView = ((WebView)wv);
  84.  
  85.         webView.loadUrl("http://www.google.at");
  86.         webView.setWebViewClient(new MyWebViewClient());
  87.  
  88.  
  89.  
  90.         return v;
  91.     }
  92. }
  93.  
  94. public static class MyWebViewClient extends WebViewClient {
  95.  
  96.     @Override
  97.     public boolean shouldOverrideUrlLoading(WebView view, String urlString) {
  98.  
  99.         handleURLRequest("modal");
  100.         return false;
  101.     }
  102.  
  103.  
  104.     private void handleURLRequest(String transitionStyle){
  105.  
  106.         // here I want to replace the Fragment with a new one.
  107.  
  108.     }
  109. }
  110.  
  111. }
clone this paste RAW Paste Data