Question

Hi everyone i'am trying to set a progressbar while in fragment transition, my first fragment onClick invokes the second, which will do some heavy work before it displays. This heavy work doesn't use asynctask because is a parse from html that is in cache.

Whats the best aproach do make this work?!

my first fragment:

private void onClick(final MoodleCourseContent[] courseContent,
        View layout, final int contentIdx) {

    layout.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String courseId = Integer.toString(v.getId());
            String courseName = courseContent[contentIdx].getName();
            String topicId = (String) v.getTag();

            Bundle bundle = new Bundle();
            bundle.putString("courseId", courseId);
            bundle.putString("courseName", courseName);
            bundle.putString("topicId", topicId);

            FragmentManager fragmentManager = getActivity()
                    .getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager
                    .beginTransaction();

            FragTopics insideTopicsFrag = new FragTopics();
            insideTopicsFrag.setArguments(bundle);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction
                    .replace(R.id.mainFragment, insideTopicsFrag);
            fragmentTransaction.commit();
        }
    });
}....

my second fragment:

public class FragTopics extends Fragment {

Object course;
// ManSession Manager Class
ManSession session;
String courseId;
Long topicId;
String courseName;

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onViewCreated(view, savedInstanceState);
}

public FragTopics() {
}

@Override
public void onAttach(Activity activity) {
    // TODO Auto-generated method stub
    super.onAttach(activity);
}

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

    session = new ManSession(getActivity().getApplicationContext());

    courseId = getArguments().getString("courseId");
    topicId = Long.parseLong(getArguments().getString("topicId"));
    courseName = getArguments().getString("courseName");

    MoodleCourseContent[] courseTopics = new ManContents(getActivity()
            .getApplicationContext()).getContent(courseId);

    MoodleCourseContent singleTopic = new ManContents(getActivity()
            .getApplicationContext()).getTopic(topicId, courseTopics);

    return createTopics(singleTopic, courseName, courseId, topicId);
}......
Was it helpful?

Solution

The best approach is the do the heavy lifting in another thread/AsyncTask. Even if you get a progress bar displayed correctly, you still risk your app being terminated with 'Application Not Responding' if you aren't responsive enough on the UI thread.

That said, the easiest way to display a progress bar would be to make it part of the hosting activity's layout that's gone by default. The first fragment sets it to visible before starting the second fragment. The second fragment, once loaded, finds the progress bar and sets it back to gone.

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