0

It might be an easy question but I have been working on it for hours and couldn't solve it. Sorry if it was asked before.

In my activity I have 2 Fragments. In one fragment I am getting JSON from server and putting in ArrayList. After putting in ArrayList I have to add another fragment passing first value in ArrayList.

Here is the problem, I want to add Fragment when my ArrayList is completed, after it got all values from server. I am making service call in my onActivityCreated() method, server call is happening in another class and I am getting Bundle in a method called onRequestFinished() and I am putting JSON to ArrayList in this Overridden method.

P.S. I tried to put AsyncTask but I couldn't make it since I have to wait the response from onRequestFinished() method.

How can I handle it and complete Fragment Transaction after filling my ArrayList?

Thanks

Gokhan Arik
  • 2,626
  • 2
  • 24
  • 50

1 Answers1

0

I suggest using startActivityForResult in the first fragment. When the parent activity receives the onActivityResult it can then start the second fragment. There are plenty of examples of how to do this on SO and elsewhere.

Even if you do not want to start an activity, you can use the same mechanism to communicate back to the activity when your first fragment has completed e.g.:

Is there a method that works like start fragment for result?

Alternatively you could of course just implement a callback mechanism from your first fragment - but personally, I like the onActivityResult approach.

Community
  • 1
  • 1
IanB
  • 3,489
  • 1
  • 20
  • 24
  • But I am not starting any Activity in my Fragment? I have an activity, and this Activity has two fragments. In first fragment I am getting some data from server and depending on this data I am calling second fragment. My activity doesn't end, it is still active – Gokhan Arik Jul 19 '13 at 13:55
  • I've clarified my answer. – IanB Jul 19 '13 at 14:43