简体   繁体   中英

OnTouchListener not working fine

I have some problems with OnTouchListener . I want that everytime when user touch the screen to go to a certain activity. The problem is that when I touch the screen it is making something like an refresh for the present activity and it starts (the current activity contains a ViewFlipper and when the screen is touched the viewflipper starts flipping from the begining). Only at the second touch the app goes to next activity. Can anyone help me to solve this?

My current activity extends OnTouchListener , I set myviewflipper.setonTouchListener(this) and my onTouch() method is :

@Override
    public boolean onTouch(View v, MotionEvent event) {
            finish=true;
        Intent intent = new Intent(getBaseContext(), FinishApplication.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
        return true;
    }

After I touch the screen is called onPause() and onResume() . On onResume() method I start some timers for some updates (when updates are made, the slideshow starts) and I think that's the reason why my activity is refresh. I set a variable boolean finish to true only after the screen is touch and start the updates only if finish is false but is not working. My variable is not set to true. Any other idea how can I solve this ?

@Override
    protected void onResume() {
        super.onResume();
        System.out.println("OnResume ViewPlaylist!!");
        t = new Timer();
        t.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Imalive();
                    }
                });

            }

        }, 300, 30000);

        if (finish != true) {
            System.out.println("Start update!");
            update = new Timer();
            update.scheduleAtFixedRate(new TimerTask() {

                @Override
                public void run() {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            updateItems();
                        }
                    });
                }

            }, 30000, 20000);
        } else {
            finish = true;
            Intent intent = new Intent(SlideShow.this, FinishApplication.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            finish();


}
}

我通过在onPause()方法上调用finish()解决此问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM