I am new to Android development.
How to write onTouchEvent()
in one activity and access in all other activities in the application?
I am trying in following way:
Public class TouchActivity extends Activity
{
@Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
boolean touch_up_detect;
if (action == MotionEvent.ACTION_UP)
{
//touch_up_detect = true;
if (timer == null)
{
timer = new Timer();
timer.schedule(new ScheduledTaskWithHandeler(), 10000);
}
}
else if (action == MotionEvent.ACTION_DOWN)
{
System.out.println("user touching screen**********");
if (timer != null)
{
timer.cancel();
timer.purge();
}
}
return false;
}
class ScheduledTaskWithHandeler extends TimerTask {
@Override
public void run() {
// TODO Auto-generated method stub
Intent intent = new Intent(TouchActivty.this, MainActivity.class);
startActivity(intent);
}
}
}
public class DashboardActivity extends TouchActivity
{
}
Whenever I am touching the dashboard activity, I am not able to access onTouchEvent()
from TouchActivity
.
That is because you are extending the activity with DashboardActivity. This means that, as far as your device is concerned, these are two separate functions. Activities are roughly separate beasts that are independent from one another.
If you just want to manipulate the timer in your Touch Activity, you could start an activity for result and pass the timer in as an argument. I think this will achieve what you are asking for. Here you can find a question which talks about passing data between activities.