android - method called by javascript interface wont update view -


been stuck on while now. have webviewfragment has html file, created, loaded. in html file javascript allow me call methods in android application. here html:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title></title> </head>  <body> <img src="guideline1.gif" usemap="#guideline9" border="0" /> <map name="guideline9" id="generalguideline9">  <area shape="rect" coords="124,329,290,357" onclick="android.testmethod();" /> </map>   </body> </html> 

the webviewfragment code looks this:

@suppresslint("javascriptinterface") public class mywebview extends webviewfragment {  android.webkit.webview wv; string name2; string filename2; string itemfilename;  communicatewithmywebview mcallback;  public interface communicatewithmywebview {     public void toggleactionbar();     public void addtab(); }  @override public void onattach(activity activity) {     super.onattach(activity);      try {      mcallback = (communicatewithmywebview) activity;     } catch (classcastexception e) {         throw new classcastexception(activity.tostring()                 + " must implement communicatewithmywebview");     }  }    @suppresslint("setjavascriptenabled") @override public void onresume() {     super.onresume();      name2=getarguments().getstring("name2");      filename2=getarguments().getstring("filename2");     itemfilename = "file:///android_asset/" + filename2 + ".html";      wv = getwebview();      wv.getsettings().setjavascriptenabled(true);      wv.addjavascriptinterface(new webappinterface(this.getactivity()), "android");       wv.getsettings().setbuiltinzoomcontrols(true);      wv.getsettings().setloadwithoverviewmode(true);      wv.getsettings().setusewideviewport(true);      wv.setlongclickable(true);      wv.setonlongclicklistener(new onlongclicklistener() {          public boolean onlongclick(view v) {              mcallback.toggleactionbar();             return false;         }     });      wv.loadurl(itemfilename); }  public class webappinterface {     activity mcontext;      webappinterface(activity activity) {         mcontext = activity;     }      @javascriptinterface     public void testmethod() {           mcallback.addtab();     } }  

}

this gets me point of having webviewfragment calling method of parent activity, working. mainactivity implements communicatewithmywebview interface. here portion of main activity code has addtab() method:

public void addtab() {      tabsfrag.test1();  } 

tabsfrag fragment being displayed mainactivity. here code test1() function in tabsfrag:

public void test1() {      log.d("myapp", "" + b1.gettext()); } 

b1 button inside of tabs frag. code works fine have shown it. if change log command :

public void test1() {      b1.setvisibility(view.gone); } 

given b1's visibility visible, won't change visibility on emulator (but won't crash, api 18), stall , crash on tablet have test on . samsung galaxy tab running 4.0.4.

if call test1() method in tabsfrag other method, long action wasnt originating map area click in html file, setting visibility works fine no crashes or anything.

i'm not sure how implement try catch blocks here.

any great.

use ui thread change view state.

@javascriptinterface     public void testmethod() {          runonuithread(new runnable(){              public void run(){                mcallback.addtab();             }          });      } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

c++ - End of file on pipe magic during open -