Skip to content Skip to sidebar Skip to footer

If An Activity Takes Long Time To Load, It Asks For Force Close, How To Solve This?

Almost all the activities in my application need to interact with a web server to get data and then load the Activity screens. So at any moment if the internet is slow, it takes a

Solution 1:

By Optimizing your code, Secondly do your Network/Internet work on a separate thread(like AsynTask).

Link: http://developer.android.com/reference/android/os/AsyncTask.html

This will not block your UI.

Hope This Help

Solution 2:

Its a problem of slow internet connections. I have also faced it earlier while making an app which used to fetch data from a web server. So there are 4 things which can help you in this matter: (1) Check if there is any internet connection or not:

ConnectivityManagercm= (ConnectivityManager)   getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfoni= cm.getActiveNetworkInfo();
    if (ni!=null && ni.isAvailable() && ni.isConnected()) {
        returntrue;
    } else {
        returnfalse; 
    }

If there is no internet connection then you can inform the user about it by a Dialog.

(2) If there is an internet connection but its very slow, then you can put some code for waiting for the response for a certain time and then showing a Dialog to user for waiting or aborting the request.

(3) One more thing which is very useful - use slide_in_left and slide_out_right anim styles in place of fade_in and fade_out for transitions between intents. It helps a lot, the screen goes black very rarely.

try    {           
        Intent yourIntent = new Intent(view.getContext(), YourActivity.class);      
        startActivityForResult(yourIntent, 0);      
        overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);        
    } catch(Exception ex) {
    }

(4) Use AsyncTask for sending the requests to web.

Solution 3:

You could check to see if there is an existing internet or data connection prior to making the call. You should also be able to specify a certain length of time to try and load the file, then display an error or try again as desired.

Post a Comment for "If An Activity Takes Long Time To Load, It Asks For Force Close, How To Solve This?"