Android: catch MotionEvent across activities
I have two activities A and B. I would like to have one touch event MotionEvent.ACTION_DOWN caught in A, while still holding down, launch B, then having the release event MotionEvent.ACTION_UP be开发者_如何学Python caught in B.
In A there is a View v that has an OnTouchListener with the following callback:
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startActivity(new Intent(A.this, B.class));
break;
case MotionEvent.ACTION_UP:
// not called
break;
}
// false doesn't work either
return true;
}
In B there is an overlapping View v2 (over the original v) with the same kind of OnTouchListener but B's "onTouch" is not getting called when activity starts unless I move my finger (regenerating touch events).
Simply put, I am doing an application that would cause a new activity to appear when holding down on the screen and finish when I release the finger.
Is it not possible to have a MotionEvent.ACTION_DOWNed state transfer from one view to another? Or does the new activity B clear any current "on screen touch listeners" only available to A because it was initiated there?
Thanks for any explanation of how these MotionEvents get dispatched to activities and/or any solution/hack to my problem.
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
startActivity(new Intent(A.this, B.class));
break;
case MotionEvent.ACTION_UP:
// Obtain MotionEvent object
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent motionEvent = MotionEvent.obtain(
downTime,
eventTime,
MotionEvent.ACTION_UP,
x,
y,
metaState
);
// Dispatch touch event to activity (make B static or get the activity var some other way)
B.OnTouchEvent(motionEvent);
break;
}
// false doesn't work either
return true;
}
And in B Activity Override OnTouchEvent (make B implements OnTouchListener) like this:
@Override
public bool OnTouchEvent( MotionEvent e )
{
return someview.OnTouchEvent( e );
}
And remember , someview must be a view in order to catch the ontouchevent , cause the activitys doesn't really knows what to do with it.
精彩评论