Issue
In a WebView-derived class I override loadUrl():
@Override
public void loadUrl(String url) {
super.loadUrl(url);
}
Inside this overriden loadUrl(), I want to know whether it was called as a result of the user touching a link (inside the same WebView), or was actually called directly somewhere in my program (either directly by me, or indirectly as a result of some Javascript processing etc.).
I was initially thinking of distinguishing between the two by setting a flag in an OnTouchListener()
:
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
// set flag !!!
}
return false;
}
and resetting it in loadUrl(), after checking its value.
The problem with this "solution" is that in the aforementioned onTouch() implementation, any touch sets the flag, so a false scenario like this is possible:
- The user touches the screen (but not a link!)
- onTouch sets the flag.
- My program calls loadUrl() directly.
- loadUrl() checks the flag and thinks that it was called as result of a touch.
So obviously the above onTouch() handling is too simplistic and doesn't work.
Any idea how to implement code that actually knows how to tell between a touch-triggered loadUrl() and a non-touch-triggered loadUrl()?
Update: I checked the stack trace suggestion below, and this is the stack trace at loadUrl() when it is invoked via touch:
com.utubefan.MyWebView.loadUrl(MyWebView.java:365)
com.utubefan.MyWebViewClient.shouldOverrideUrlLoading(MyWebViewClient.java:123)
android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
android.os.Handler.dispatchMessage(Handler.java:99)
android.os.Looper.loop(Looper.java:123)
android.app.ActivityThread.main(ActivityThread.java:4627)
java.lang.reflect.Method.invokeNative(Native Method)
java.lang.reflect.Method.invoke(Method.java:521)
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
dalvik.system.NativeStart.main(Native Method)
I am not sure that I can identify an explicit touch event there, but the following looks promising (they don't appear on a non-touch loadUrl):
android.webkit.CallbackProxy.uiOverrideUrlLoading(CallbackProxy.java:216)
android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:323)
Solution
I don't know of a proper way to do what you're asking but I can think of a way that will probably work. In your loadUrl() override, you can query your calling stack and see what routine called into you. Your caller will be one location for a touch leading to you, and anything else for having been called programatically.
http://www.javapractices.com/topic/TopicAction.do?Id=78 contains code showing how to determine your stack trace from a throwable (which you would create in your loadUrl() method).
Answered By - mah
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.