Reputation: 488
I have a problem in one work flow.
I have hybrid app. I am calling a java function which in terms opens and activity and returns. I want the return value of activity to be returned to the javascript. But what happens here is function returns before the activity finishes. I tried it with threading but it stops rendering ui. Please help...... #
/* packages and import statements */
public class Ftsbeta extends CordovaActivity {
String retURL="";
public static Thread t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.init();
t=Thread.currentThread();
appView.addJavascriptInterface(this, "MainActivity");
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.loadUrl(Config.getStartUrl());
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
@JavascriptInterface
public String customFunctionCalled(final String url) throws InterruptedException {
Log.e("Custom Function Called", "Custom Function Called");
t= new Thread(new Runnable() {
@Override public void run()
{
// do some work here
synchronized (t) {
try {
Intent i = new Intent(Ftsbeta.this, CustomWV.class);
i.putExtra("url", url);
startActivityForResult(i, 02);
} catch (Exception e) {
// TODO Auto-generated catch block
Log.e("msg","Exception");
e.printStackTrace();
}
}
}
});
t.start();
// Thread.currentThread().join();
Log.e("msg","returned");
return retURL;
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
// TODO Auto-generated method stub
// check if the request code is same as what is passed here it is 2
synchronized (t) {
if (requestCode == 2) {
// fetch the message String
if(resultCode==RESULT_OK){
retURL = intent.getStringExtra("url");
// Set the message string in textView
Log.e("url",retURL);
}
}
Log.e("here","time to notify");
t.notify();
}
}
}
# Another Activity which is being opened (Customwv.java) /* imports and package */
public class CustomWV extends Activity {
private WebView webView;
ProgressDialog d;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom);
Intent myIntent = getIntent(); // gets the previously created intent
String loadURL = myIntent.getStringExtra("url"); // will return
Log.e("here","opening windows");
webView = (WebView) findViewById(R.id.wv1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("here","opening windows2");
d = ProgressDialog.show(CustomWV.this, "Fts",
"Please Wait While Till Site Loads");
view.loadUrl(url);
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
d.dismiss();
if (url.contains("action=done")) {
Intent intentMessage = new Intent();
// put the message to return as result in Intent
intentMessage.putExtra("url", url);
// Set The Result in Intent
setResult(02, intentMessage);
finish();
}
}
});
webView.loadUrl(loadURL);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
return super.onCreateOptionsMenu(menu);
}
}
Javascript from where i am calling it.
code is : var a= window.MainActivity.customFunctionCalled(pgurl);
Please help me to do it in a proper manner.
Thanks in advance...
Upvotes: 0
Views: 335
Reputation: 2768
The whole purpose of Cordova plugin mechanism is to establish communication between native and js side. Take a look at the documentation and echo plugin example.
With plugin mechanism waiting and delaying response etc is possible. There are also several plugins to peek in github that handles the exact problems you point out.
Upvotes: 1