1

I have an app that is mainly based on fragments. Each fragment involves it's own json parsing and image loading from json.Also to be noted that I have called addToBackStack(null) on ech fragment when it's getting replaced. As I switch between the fragments, and press the back button, it takes quite some time 4-5 secs to be exact to load the previous fragment. Is there any way that the load time can be minimized?

I have used async task to fetch the data,async task is performed in fragment's onAttach() method to prevent json parsing when back button is pressed.

Nishan Khadka
  • 385
  • 1
  • 2
  • 14

4 Answers4

0

This could easily be a simulator issue, but if not it could be because you are loading all of the information on the main thread instead of in an async task. Making an Async task loads the information in the background and updates the UI when finished

0
   public void currentFragment(int position, Bundle bundle) {
        switch (position) {

            case 105:
                MainFragment workoutFragment = MainFragment.newInstance("", "");
                workoutFragment.setArguments(bundle);
                replaceFragment(workoutFragment, "front");
                break;

            case 108:

                break;
            default:
                replaceFragment(new MainFragment(), "Main");
                break;
        }
    }

    private void replaceFragment(Fragment fragment, String tag) {
        String backStateName = fragment.getClass().getName();
        try {
            boolean fragmentPopped = fm.popBackStackImmediate(backStateName, 0);
            if (!fragmentPopped && fm.findFragmentByTag(backStateName) == null) {
                //fragment not in back stack, create it.
                FragmentTransaction ft = fm.beginTransaction();
                ft.add(R.id.fragment_container, fragment, backStateName);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                ft.addToBackStack(backStateName);
                ft.commitAllowingStateLoss();
                //ft.commit();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

// inside  Backpress    @Override
   // public void onBackPressed()
//  if (fm.getBackStackEntryCount() == 1) {

//     super.onBackPressed();

// }

//  to add fragment call like this 

// currentFragment(101,null);

// what it do is popup from stack not add again and again that is best method to achiveve this if still getting problem then ask me again
0

If you have no need to reload images in fragments on backpress ,then you can use this trick

in Each Fragment

  View mView; // declare it globally

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       if(mView == null){ // it will not reload image it will set first time view which is loaded. 
          mView = inflater.inflate(R.layout.fragment_dashboard, null);
        }
        return rootView;
    }

This will be load immediately . it will restore previous view on backpress

Mayur Raval
  • 3,250
  • 6
  • 34
  • 57
0

One reason could be the amount of data that you're loading. To overcome situation, you can user Handler with Runnable.

// Declaration
private Handler layoutHandler = new Handler();
private Runnable mRunnable;

// Initialization
mRunnable = new Runnable() {
       @Override
       public void run() {
            hideProgress();
            // Perform an action such as binding your data
       }
};

Now make delay before loading a data... using

showProgress();
layoutHandler.postDelayed(mRunnable, 800);

I recommend to save your data using savedInstanceState rather then loading data everytime.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26