4

I have a fragment, which have various questions. These question are fetched dynamically. Page does not have any save button. Pages answer(s) is saved once user navigate away from page. onStop() method is used to save the answer.

Problem: I have several question pages. So suppose user saves some answer in page one and quickly navigate to page 2 and page 3, answers are not getting saved. Below is the pictorial representation to explain

Question

My QuestionPagefragment onStop() method have these calls

public void onStop()
{
    super.onstop();
    new LongOperation().execute("");
}

 private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
          TaskAdapter adapter = TaskAdapter.getInstance();
          adpater.saveDataInCache();

          //create result
          adpater.updateResults();

          //update result
          adpater.updateDatabase();
    }

    @Override
    protected void onPostExecute(String result) {

    }

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... values) {}
}

My question is, how do I save the result in onStop(). When I am adding a message dialog to indicate progress is going on, the message dialog comes up in main page not in the question page, as it is called on onStop;. Can anybody suggest me some thing .

Android
  • 3,828
  • 9
  • 46
  • 79
  • try onDetach ... check this thread .. http://stackoverflow.com/questions/9156406/whats-the-difference-between-detaching-a-fragment-and-removing-it – Sabeer Aug 07 '14 at 16:18
  • Moving this method in hierarchy will buy me some time, I am not sure about the amount of data thats required to be saved. Can you suggest me some way which will give me more specific results. – Android Aug 07 '14 at 16:20
  • Why not simply move LongOperation to the holding Activity? Calling something like ((MyActivity)getActivity()).save() in onStop/onDetach – cYrixmorten Aug 07 '14 at 16:45
  • thats what I am doing, but when fragment is stopped and while answer is getting saved in background, u start and stop another fragment, its not saving for other fragment – Android Aug 08 '14 at 04:53

0 Answers0