Doing everything in onCreateView(...)
is probably not ideal. Your best option is to move everything related to loading a question to its own method. In onCreateView(...)
, inflate your views and store local references to them. Then, override one of the fragment lifecycle methods that are garanteed to be called after that, such as onActivityCreated()
and call the loading method from there; this would get the question and populate the views with the right data. Then, whenever you want a new question, simply call that loading method to repopulate the views with the new questions and answers. I can provide pseudocode if needed.
Edit: here is some pseudocode illustrating what I mean. This is by no means valid code but it is the basis of what you should be doing.
class Fragment {
private TextView question
private TextView answer
onCreateView {
View v = inflate view
question = view.findView(question)
answer = view.findView(answer)
}
onActivityCreated {
loadQuestionAndAnswer()
}
loadQuestionAndAnswer {
JSON json = Interwebs.getJSON()
question.setText(json.getQuestion())
answer.setText(json.getAnswer())
}
Dialog.onClick {
loadQuestionAndAnswer()
}
}