0

For some reason my tab transition is not smooth. I have coded two separate AsyntTaskLoaders in two separate fragments (1 & 2) to read data from WEB API (On button clicks).

Once the data is loaded in second fragment going to the previous tab is slow (and vice versa). But when the Loader is destroyed the transition is smooth.

public class Fragment1 extends Fragment implements LoaderManager.LoaderCallbacks {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    if(getLoaderManager().getLoader(0)!=null){
        getLoaderManager().initLoader(0,null,this);
    }

     Submit.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
             if(some condition) {

            }

            else{
                stopLoader();
            }
        }
    });

}

@Override
public Loader<String> onCreateLoader(int id, Bundle args) {
    return new GetDataFromLoader1(getContext(), args.getString("String"));
}

@Override
public void onLoadFinished(Loader<String> loader, String data) {
    try {
        //Read parsed JSON object from LoadInBackground

    }catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public void onLoaderReset(Loader<String> loader) {       
}


 void stopLoader() {
    getLoaderManager().destroyLoader(0);       
}}

Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread. While reading this line in " MyBitmap = obj.getBitmap();" in JSON object

gorp88
  • 105
  • 14

1 Answers1

0

Loaders offload all their hard work to background threads, which is good, and will not cause your app UI to glitch.

However, I suspect because your loader only loads String JSON and you parse it on the main thread, which causes your UI to drop frames, or become not smooth.

Try returning your model parsed from the loader instead of parsing the result in onLoadFinished. And do your loading and parsing in loadInBackground

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • I figured out the problem, it was throwing this message in logcat "Choreographer: Skipped 31 frames! The application may be doing too much work on its main thread." which means I need to separate my loader from the main thread? Any idea how to setImageBitmap inside OnLoadFinished without overloading main thread. – gorp88 Jan 05 '18 at 23:36
  • Skipping frames means you are doing something that takes too long on the main thread that is preventing it from doing its job (drawing the UI) consider showing more code in that class to be able to help identify where – elmorabea Jan 06 '18 at 03:01