Here I am trying to get clipboard text I have tried both ways first i tried with clipboard and second with sharedpreferences in both the cases behaviour is same when i select the word from my custom webview and copy that word using jsinterface i got the word sucessfully but when immideatly aftre that i try to get that world it give me anoter world suppos first i copy word "A" then I copy word "B" and then immideatly try to get "B" It gives me "A" here is my code inside webviews Action Item clicked
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch (item.getItemId()) {
case R.id.action_done:
getSelectedData();
mode.finish();
return true;
break;
}
}
and my getSelectedData() function
private void getSelectedData() {
String js = "(function getSelectedText() {" + "var txt;"
+ "if (window.getSelection) {"
+ "txt = window.getSelection().toString();"
+ "} else if (window.document.getSelection) {"
+ "txt = window.document.getSelection().toString();"
+ "} else if (window.document.selection) {"
+ "txt = window.document.selection.createRange().text;" + "}"
+ "JSInterface.getText(txt);" + "})()";
// calling the js function
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
evaluateJavascript("javascript:" + js, null);
} else {
loadUrl("javascript:" + js);
}
showDialog();
}
in this function getText is function from javascriptInterface and my webappInterface class is as under
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
@JavascriptInterface
public void getText(String text) {
// put selected text into clipdata
ClipboardManager clipboard = (ClipboardManager)
mContext.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text",text);
clipboard.setPrimaryClip(clip);
sp.edit().putString("COPIED_TEXT",text).commit();
}
}
and In my showDialog function I am trying to get text which is put in sharedpreferences and clipboard text but it get previously saved text as i described above how to resolve this issue ihave tried both way in showDIalog Function like this but no proper working found
//ClipboardManager clipboard = (ClipboardManager) context.getSystemService(context.CLIPBOARD_SERVICE);
//String selectedClipboardText = "";
//selectedClipboardText = (String) clipboard
// .getText();
String selectedClipboardText = "";
selectedClipboardText=BilingualNews.BNPref.getString("COPIED_TEXT", "");
selectedClipboardText = getFilteredSelfTextWord(selectedClipboardText);