0

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);
}......
firetrap
  • 1,947
  • 3
  • 24
  • 39
  • 1
    If you have a lot of heavy computation, the best approach would probably be to do it in an asynctask after all, or in another thread. Otherwise you won't be able to render your fragment until your computation is completed and then you won't be able to show the progressbar. – Catherine Nov 02 '13 at 20:09
  • Yes i'am assuming that's the only way to go i'am avoiding that because I've to change lot of things but if is no other way i will have to do it – firetrap Nov 02 '13 at 20:34

1 Answers1

2

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.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
  • yesterday i tried to implement the asynctask with fragment but is not working well, can you give some help with this? some example with the asyncTask – firetrap Nov 06 '13 at 13:26
  • can you have a look on this question is related with this and i'am stuck http://stackoverflow.com/questions/20460170/view-overlap-after-updating-fragment-with-asynctask – firetrap Dec 08 '13 at 23:48