Android onTouchListener Action_Up-MotionEvent delayed after first resume

I am new to Android and Java programming and have been able to find many solutions to my many questions. However, I am now stuck not finding any solutions on the internet.

I wrote a simple application that creates an onTouchListener in the onCreate method of my MainActivity. info () starts a new InfoActivity.

If I kill my app and run it the first time, the first time I touch the info ImageView, I see ACTION_DOWN and ACTION_UP in quick succession and the new InfoActivity opens quickly.

However, if I resumed MainActivity, either by minimizing it, or by visiting InfoActivity and returning (with finish ()), I now notice a huge delay of about 2000ms between the ACTION_DOWN and ACTION_UP events. The new activity is also deferred in the same way, even if I move the info () call in the case of ACTION_DOWN like in this code.

As far as I can tell, nothing happens during those two seconds of delay.

Please answer with a method, how can I fix this better. I realize there is probably an easy way to find out, but I've literally spent hours searching and nothing I've tried has brought me closer to a solution.

Regards

Julius

    info = (ImageView) findViewById(R.id.info);
    info.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View arg0, MotionEvent arg1) {
            switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    Log.i("MOTION_EVENT","ACTION DOWN");
                    info.setImageResource(R.drawable.infoback);
                    info();
                    Log.i("MOTION_EVENT","ACTION INFO");
                    break;
                }
                case MotionEvent.ACTION_CANCEL:{
                    Log.i("MOTION_EVENT","ACTION CANCEL");
                    info.setImageResource(R.drawable.info);
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    Log.i("MOTION_EVENT","ACTION UP");
                    info.setImageResource(R.drawable.info);
                    break;
                }
            }
            return true;
        }
    });
    Log.i("TIMESTAMP","End onCreate");

      

Here is the log of starting the app, opening infoActivity, closing infoActivity and then trying to do it again, experiencing slowdown.

06-29 02:12:12.529  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ Start onCreate
06-29 02:12:13.083  15856-15856/de.juliusbier.shamebell E/MediaPlayer﹕ Should have subtitle controller already set
06-29 02:12:13.083  15856-15856/de.juliusbier.shamebell I/TIMESTAMPEnd onCreate
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/TIMESTAMPStart onResume
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/SHAKER﹕ RESUME
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/TIMESTAMPEnd onResume
06-29 02:12:13.089  15856-15877/de.juliusbier.shamebell D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-29 02:12:13.092  15856-15856/de.juliusbier.shamebell D/Atlas﹕ Validating map...
06-29 02:12:13.099  15856-15856/de.juliusbier.shamebell E/MediaPlayer﹕ Should have subtitle controller already set
06-29 02:12:13.128  15856-15877/de.juliusbier.shamebell I/Adreno﹕ EGLInit: QTI Build: 01/29/15, 1bccc5d, I0ba6dce82d
06-29 02:12:13.138  15856-15877/de.juliusbier.shamebell I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-29 02:12:13.144  15856-15877/de.juliusbier.shamebell D/OpenGLRenderer﹕ Enabling debug mode 0
06-29 02:12:20.480  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION DOWN
06-29 02:12:20.521  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION INFO
06-29 02:12:20.557  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION UP
06-29 02:12:20.563  15856-15856/de.juliusbier.shamebell I/INFO﹕ Start onCreate
06-29 02:12:20.578  15856-15856/de.juliusbier.shamebell I/INFO﹕ Finish onCreate
06-29 02:12:24.992  15856-15856/de.juliusbier.shamebell I/TIMESTAMPStart onResume
06-29 02:12:24.993  15856-15856/de.juliusbier.shamebell I/SHAKER﹕ RESUME
06-29 02:12:24.993  15856-15856/de.juliusbier.shamebell I/TIMESTAMPEnd onResume
06-29 02:13:03.367  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION DOWN
06-29 02:13:03.375  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION INFO
-----------------------------Here comes the delay-------------------------------------
06-29 02:13:05.540  15856-15856/de.juliusbier.shamebell I/MOTION_EVENT﹕ ACTION UP
06-29 02:13:05.542  15856-15856/de.juliusbier.shamebell I/INFO﹕ Start onCreate
06-29 02:13:05.551  15856-15856/de.juliusbier.shamebell I/INFO﹕ Finish onCreate

      

UPDATE: Making calls to setImageResource solves the problem, it seems to me that the drawing is somehow delayed and no new activity is opened until all the setImageResources functions have been considered.

        info = (ImageView) findViewById(R.id.info);
        info.setImageResource(R.drawable.info_selector);
        info.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                shaker.close();
                Intent infoIntent = new Intent(getBaseContext(), InfoActivity.class);
                startActivity(infoIntent);
            }
        });

      

With this code from Rajesh Batth I get the same problems. Note that the code in onClick (View v) is my old info () method. shaker.close calls the following line in the jitter detection class that I adapted from this post 's answer :

        mgr.unregisterListener(listener);

      

Do not close the shaker or even remove the shake detection completely changes nothing.

+3


source to share


3 answers


It has nothing to do with your onTouch as it works fine. The point is when onResume () is called, which means your activity is resumed. Super.onResume () gets called, and the line actually does something when you touch the screen. Ondispatchtouch gets called with calls to your barcode line and does something - it's all on a specific thread and both are executed sequentially, hence the wait. In short, your onResume () does a lot of code and until its made calls to android on the main thread are frozen.

Edit-after-your-comment- & -edit



Sir, I still support when the thread is busy, even though I was not the reason for the lag. If this happens when you are coming from onResume () it is because android is loading your required resources before proceeding, as it setImageResource()

works on the UI thread and also depends on the image size, so I think your problem was resolved after commenting or withdrawingsetImageResource()

+1


source


If you are doing some tedious operation inside info (), this can happen. Also looking at the code it feels like the requirement can be achieved with onClickListener and selector drawable. The fetch selector would be like this. info_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/infoback"/>
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/infoback"/>
<item android:state_focused="true" android:drawable="@drawable/infoback"/>
<item android:state_focused="false" android:state_pressed="false" android:drawable="@drawable/info"/>
</selector>

      



In Actitity

info = (ImageView) findViewById(R.id.info);
info.setImageResource(R.drawable.info_selector);
info.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
       info();
    }
});

      

+1


source


We had exactly the same behavior, and in our case the solution was simply to put

setFocusable(true);

      

in the constructor of your SurfaceView (which you are probably in there somewhere).

In our case, the SurfaceView looks like this:

public class GameSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

private GameActivity controller = null;
private InputComponent inputHandler = null;

public GameSurfaceView(GameActivity controller) {
    super(controller);
    this.controller = controller;
    getHolder().addCallback( this );
    setFocusable(true);
    Log.d("GameSurfaceView", "constructor.");
}
}

      

This little line with setFocusable helped some strange reasons. However, I don't know why there is no delay when the application is first launched.

Greez

0


source







All Articles