0

in my code lets say that I want to display some string in a fragment, and method getPlayerList takes 2 seconds(data from API), but method 'onCreateView' is returning the view before 'getPlayerList' finishing its execution.

I tried to implement it in many ways, but every thing was useless because I can't change 'return view;' place ^^".

is there any method instead of 'onCreateView' that suits my situation ?

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_tab4, container, false);

String result = getPlayerList();
//displaying the 'result' on this view

return view; }
Cocodile
  • 11
  • 6

2 Answers2

1

So, as I can see: the problem for you is that the result variable does not have any value when you are trying to use it? If so, consider using that variable only after completion API call, so that your user don't have to wait 2 seconds ( or more in some scenarios). You can add ProgressBar to indicate your user that something is in progress.

Maksym V.
  • 2,877
  • 17
  • 27
  • as order of code, the method 'getPlayerList' will return the value before using the variable, but what actually happening, is the run call 'getPlayerList' then continue before the method finishing its execution. – Cocodile Feb 14 '18 at 21:59
  • @Haifa what you use for API call? – Maksym V. Feb 14 '18 at 22:05
0

Do not request and wait in main thread. first of all keep that view variable as a field instead of local. then move your request to onViewCreated method. and finally use callback instead of return value and do all request things in background thread. you can use Volley or Retrofit for high level networking. also because you have to wait until get result from server fill the UI with placeholder your empty strings before you set them.

check this example in MVP structure from google. the view created in method but the value of ui set in another method later. for example see the showDescription method that set the description textview https://github.com/googlesamples/android-architecture/blob/todo-mvp/todoapp/app/src/main/java/com/example/android/architecture/blueprints/todoapp/taskdetail/TaskDetailFragment.java

also for callback see this public void success method in Retroift. https://futurestud.io/tutorials/retrofit-getting-started-and-android-client

you get value in a method and you can set that value with the fields of view.

Siavash Abdoli
  • 1,852
  • 3
  • 22
  • 38
  • if you need any clarification leave a comment so I will update my answer – Siavash Abdoli Feb 14 '18 at 21:29
  • thank you for your replay, but what do you mean of "use callback instead of return value", I have to initialize the view in 'onCreateView' to use it in some statements before return it, and 'onViewCreated' is a void method. – Cocodile Feb 14 '18 at 21:56
  • there is no need to init the view. just create as you created. but set the value later. wait i'll attach an example – Siavash Abdoli Feb 14 '18 at 22:14
  • there is too much things you need to know for solving problem. see the retrofit tutorial. also see some opensource code and async and sync problem. the good point of retrofit is it will handle the thread and async part of your problem. check the update on answer – Siavash Abdoli Feb 14 '18 at 22:23