Question

I have a simple android Activity with an OnTouchListener. There I have a button, which starts another Activity and finishes the current one (with finish();). Now the problem is that while the next Activity is loading (and the screen is black) the Touch-events already get processed, so when the loading is done, some things are pressed already. I want the TouchListener to start working when the Activity is done loading and not halfway through.

I already tried delaying the View.setOnTouchListener(this); but it doesnt work. Touch events still get processed before the new Activity even started.

Here is an example:

public class Testclass extends Activity implements OnTouchListener {

    GLSurfaceView glsv;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        glsv = new GLSurfaceView();
        glSurfaceView.setOnTouchListener(this);
    }

    public boolean onTouch(View v2, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            startNextActivity();
            finish();
        }
        return true;
    }
}

So if this Activity would start another instance of itself through 'startNextActivity();' and if I doubletapped the screen, the second Activity would close itself too (right after its done loading) and start a third Activity.

All I want is that the second Activity only responds to Touch events after it is done loading. How do I achieve this?

and obviously the example code is not completed but I think all the relevant parts are shown.

Was it helpful?

Solution

The best solution I have found to this type of issue is achieved by leveraging ProgressDialog:

In onCreate of 2nd Activity:

final ProgressDialog progress = new ProgressDialog(getActivity());
//Prevents from any clicks outside ProgressDialog being detected
progress.setCanceledOnTouchOutside(false); 
progress.setTitle("Please Wait"); //Or whatever you want it to say
progress.setMessage("Loading Second Activity...");
progress.show();

and then once the all of the data for this Second activity has been loaded call:

progress.dismiss();

This should not only give your users feedback on what is happening with the blank screen they are seeing, but also disables them from generating any OnTouchEvents to be processed until the screen is fully loaded.

Let me know if you have any trouble implementing this solution and I'd be happy to help.

OTHER TIPS

You can add a boolean flag at the end of the OnCreate method of your activity, and use it to force the touch event to wait until it becomes true.

For example:

boolean flag = false;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    glsv = new GLSurfaceView();
    glSurfaceView.setOnTouchListener(this);
    flag = true;
}

public boolean onTouch(View v2, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        flag = false;    //Just use it if you cal this activity again on startNextActivity()
        startNextActivity();
        while(!flag){}    //Wait for flag to become true
        finish();
    }
    return true;
}

This way, flag will become true when activity it's created.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top