3

I am pretty new to Android and Java programming and was able to find a lot of solutions to my many questions here. However I find myself stuck now, without finding any solutions online.

I have written a simple app, which creates an onTouchListener in the onCreate-method of my MainActivity. info() starts a new InfoActivity.

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

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

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

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

Best 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's a log of starting the app, opening infoActivity, closing infoActivity and then trying to do so again, experiencing the 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/TIMESTAMP﹕ End onCreate
06-29 02:12:13.084  15856-15856/de.juliusbier.shamebell I/TIMESTAMP﹕ Start 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/TIMESTAMP﹕ End 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/TIMESTAMP﹕ Start 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/TIMESTAMP﹕ End 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: Taking out the setImageResource calls solves the issue, it seems to me like the drawing is somehow delayed and the new activity is not openened until all setImageResources have been dealt with.

        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. Notice that the code in the onClick(View v) is my former info() method. shaker.close calls the following line in a shake detection class I adapted from the answer of this post:

        mgr.unregisterListener(listener);

Not closing the shaker or even taking out the shake detection completely does not change anything.

Community
  • 1
  • 1
Julius B
  • 101
  • 1
  • 5

3 Answers3

1

This has nothing to do with your onTouch specifically as it works normal. The thing is when onResume() is called which means your Activity resumes super.onResume() is called, and the line actually does something, when you touch the Screen ondispatchtouch is called with calls your touch code line and does something-all this on a particular Thread,and both runs in a succession-hence the wait. In short - your onResume() does a lot of code and until its done calls to android on the main Thread a pretty frozen

Edit-after-your-comment-&-edit

Sir, i still stand by the thread being busy, even though where i was approaching from was not the cause of the lag. If this happens when you come from onResume() well it is because of the android loading your required resources before carrying on, since setImageResource() actually runs on the UI thread, and also dependent on the size of the image that is why i think your problem was solved after commenting or taking out setImageResource()

Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Thank you for your answer.I have a timestamp at the end of onResume which usually fires right away. Also I have tried waiting a good while before touching with no improvement, which leads me to believe the thread being busy from onResume() is not the answer here. – Julius B Jun 29 '15 at 09:12
  • thanks sir, but that issue only comes up even the thread is doing other task, check my edit sir, it might not be from onResume() but general loading of your resource@JuliusB – Elltz Jun 29 '15 at 13:12
1

If you are doing any time-consuming operation inside info() this could happen. Also looking at the code it feels like the requirement can be achieved with onClickListener and selector drawable. A sample 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();
    }
});
Rajesh Batth
  • 1,672
  • 2
  • 19
  • 23
  • Thank you for your answer. This is indeed a simpler implementation of a pressable button, which I will use in the future. However the problem does not lie with info(), I just implemented your solution without the selector as a simple onclick event and it worked fine. As soon as I added the selector though I had the same issues. This leads me to believe that the program seems to take a long time to draw the changed button and waits for it before opening the new activity. – Julius B Jun 29 '15 at 09:05
  • may I know what are you doing inside info()? – Rajesh Batth Jun 29 '15 at 09:07
0

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

setFocusable(true);

into the constructor of your SurfaceView (which you probably have 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 for some weird reason. However, I don't know, why the delay does not occur at the first start of the app.

Greez

ElectRocnic
  • 1,275
  • 1
  • 14
  • 25