0

I am sending an HTTP request to some server(in a background thread) inside onCreateView() method within my fragment class. When the request received i check some stuff and according to that i add a new fragment(through fragment transaction in the UI thread). I use the support library

But, for example, if the user press the android home button, while the request hasn't received yet, the fragment is going to pause or stop state and then the request received and of course the exception is thrown(just after trying to commit).

I searched the web and found some very good articles and answers which are relevant to this issue and all the things related to the 'state loss' after onSaveInstanceState() has been called, etc... For example i read this excellent article of Alex Lockwood: http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html and also those stackoverflow questions/answers: getting exception "IllegalStateException: Can not perform this action after onSaveInstanceState" and: "Can not perform this action after onSaveInstanceState" - why am I getting this exception from my activity's onResume method? and more.

But i couldn't came to a conclusion of what to do in my case. The only thing that came to mind is to use commitAllowingStateLoss() instead of commit() but this fills hacky and not correct as some of the answers that i read conclude about it. Also there was a suggestion to commit the transaction in onCreate() because it safe, but of course it can't work in my situation, all other suggestions doesn't work for me as well.

Maybe i can use some boolean flag to check if i can't make a transaction and then wait for the fragment to resume and if the flag is true then do the transaction. This fills to much work and also presents the problem in which i need to know if the transaction can be done(don't even know how to check it).

Community
  • 1
  • 1
Elizabeth
  • 95
  • 1
  • 7
  • Use a flag and set it to true in onSaveInstanceState. When the request returnes chech the flag. If its true do not commit the transaction. Commit it in onResume. – Goran Horia Mihail Nov 01 '14 at 18:58
  • I don't exactly understand you, do you mean set the flag to true in onSaveInstanceState() and when the request returns then NOT commit the transaction if the flag is set because we are after onSaveInstanceState() which means that if i will commit it i will get the above exception. And if this is the case then i need to commit it in onResume? – Elizabeth Nov 01 '14 at 20:05
  • Exactly. when the request returnes you check the flag. If its true it means we are in backgroud and you do not commit thus avoid the exception. When onResume (I think you can also use onResumeInstanceState) gets called it meanse we have returned from background and it is safe to commit now. – Goran Horia Mihail Nov 02 '14 at 06:03
  • Two things: 1. I dont think that if i set a flag in onSaveInstanceState() then it means that my app is in the background. – Elizabeth Nov 02 '14 at 09:05
  • 2. I dont think there is a method onResumeInstanceState() maybe you meant onRestoreInstanceState() which i am not sure how it will help me – Elizabeth Nov 02 '14 at 09:31
  • Yeah that what i meant I'm typing from my phone. – Goran Horia Mihail Nov 02 '14 at 09:52
  • Ok, then how would you do it? I'm always interested in learning new perspectives. – Goran Horia Mihail Nov 02 '14 at 10:34
  • I don't know. If no one will give a better solution they i probably will go with you solution. Thanks anyway. – Elizabeth Nov 02 '14 at 10:53
  • I also facing same issue after pressing Home Button while commit new fragment..did you found any luck yet. ? – CoDe Nov 27 '14 at 09:39
  • No , but maybe look at the links that i added in the question. – Elizabeth Nov 27 '14 at 16:45
  • Goran Horia Mihail: Your suggestion worked perfectly for me, thanks! – W.K.S May 04 '15 at 06:41

0 Answers0