0

I created a public global variable, I assigned data it from a subclass and I want to use that data in the onCreate method. The problem is that the variable returns null in the onCreate method and returns the data in other subclass. Am just trying to get value from javascript and store it in Java variable in the onCreate method. I am relatively new to Java and android development. Any help is appreciated.

public class MainActivity extends AppCompatActivity {

// Initiallising the WebView
private WebView mywebView;

public String userData;

private GoogleApiClient client;

@SuppressLint("JavascriptInterface")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Webview
    mywebView = (WebView) findViewById(R.id.myWebView);
    WebSettings webSettings = mywebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mywebView.setWebViewClient(new MyWebViewClient());
    mywebView.addJavascriptInterface(new JavascriptBridge(this), "AndUser");

    mywebView.loadUrl("file:///android_asset/www/index.html");

    Toast t = Toast.makeText(getApplicationContext(), userData+" Tesing..." , Toast.LENGTH_LONG);
    t.show(); // returns null Testing...
}

public class JavascriptBridge{
    Context mContext;
    JavascriptBridge(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public String getUserDetails(String message){
        userData = message;
        return message;
    }
}

}

3
  • Perhaps this SO post can help you stackoverflow.com/questions/21749425/… Commented Nov 12, 2016 at 1:58
  • If you really have to get the variable from javascript in your onCreate method, you could add a callback interface to the constructor of JavascriptBridge. Commented Nov 12, 2016 at 2:01
  • Try using an interface. Commented Nov 12, 2016 at 2:11

1 Answer 1

1

I created a public global variable, I assigned data it from a subclass and I want to use that data in the onCreate method.

Java used a oops concept for accessing parent variable that can change your parent value : i.e

// parent
public class Parent {
    private int value = 0;
    public Parent( int initialValue ) {
        value = initialValue;
    }

    public void print() {
        System.out.println(value);
    }
}

// child class
public class Child extends Parent{
    public Child() {
       super( 1 ); /// This super keyword change your parent class variable.
    }
}

Perhaps if you have issue in create bridge than you can go through this Post.

Sign up to request clarification or add additional context in comments.

1 Comment

Please @QuokMoon can you use my code to explain cos I am trying to wrap my head around the super() function. Like i said am completely new to Java. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.