Search code examples
javaandroidjakarta-eeandroid-buttonandroid-networking

when button is clicked many times, application becomes un-responsive


I'm working on a android application in which one button is there which is used for fetching data from server and show into TextView, but the problem is, if a user is clicking many times on a same button the application becomes un-responsive in Android.


Solution

  • if a user is clicking many times on a same button the application becomes un-responsive in Android

    To avoid application becomes un-responsive on Button click:

    1. Show ProgressBar on Button click and dissmiss it after getting response from server.

    2. Use AsyncTask for getting data from server in background Thread because if we put network related code on main thread then app will becomes un-responsive until response is not return from server

    3. Also handle button click when server request is already in progress by showing proper message like "In progress...".

    Use a boolean varaible for handling click event :

    boolean isTaskRunning=false;
    

    on button click before starting AsyncTask check isTaskRunning status:

       if(!isTaskRunning){
           isTaskRunning=true;
           // start task for getting data from server
    
        }else{
             //... In progress...
        }
    

    After getting data from server make isTaskRunning variable to false