-1

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.

2
  • 3
    "its very urg". NO. Saying that you deserve a faster answer because your question is "urgent" doesn't work here. If you want a faster answer, edit your question to be as answerable as possible, and if need be, start a bounty (after earning sufficient rep from answers)
    – nanofarad
    Jun 20, 2015 at 14:22
  • All your activity should extend TouchActivity you created and touch event will be accessible Jun 20, 2015 at 14:24

1 Answer 1

0

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.

3
  • 1
    Got it, we have to call super() in dashboard activity Jun 22, 2015 at 4:42
  • @GunaSekhar I'm glad you figured it out! I hope I helped in some way.
    – Rose R.
    Jun 22, 2015 at 12:43
  • @ Rose Reatherford, Thanks for helping :) Jun 22, 2015 at 13:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.