title: Android Declare global variable fragment
body: <p>I'm trying to declare a global variable but without success. I was following <a href="http://ganeshtiwaridotcomdotnp.blogspot.com/2012/04/declare-global-variable-in-android.html" rel="nofollow">this tutorial</a> but this doesn't work if I want to transfer a variable from fragment to MainActivity.</p>
  
  <p>First I added this code to my fragment class:</p>
  
  <pre><code>public class WebViewFragment extends Fragment{
  private WebView myState;
  
  public WebView getState(){
      return myState;
    }
    public void setState(){
      myState = webView;
    }
  
  }
  </code></pre>
  
  <p>And then I added this to my <code>MainActivity</code> but it doesn't work:</p>
  
  <pre><code>WebViewFragment appState = ((WebViewFragment)getApplicationContext());
  WebView state = appState.getState();
  </code></pre>
  
  <p>I get this <code>error: Cannot cast from Context to WebViewFragment</code></p>