3

In the Android Fragments Tutorial it clearly states that "Once the activity reaches the resumed state, you can freely add and remove fragments to the activity.

So technically it means that we should add and remove (or replace, I guess the tutorial's referring on the transactions) fragments to the activity during or after the onResume callback?

I find difficulty in understanding the above statement...

On the top of that I downloaded the sample "FragmentBasics.zip" to examine the code. I found that the activity is adding a fragment on the onCreate method, so before the activity reaches its resumed state.

What's going on?

Mr T
  • 506
  • 2
  • 9
  • 26

2 Answers2

0

To avoid Activity state loss, need to be sure that we do not commit fragment transactions after activity's onSaveInstanceState called. -which gets called after onStop-

You can check detail from the link below: http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html

As you can see from below:

https://developer.android.com/reference/android/app/Activity.html#onRestoreInstanceState(android.os.Bundle)

Activity's onRestoreInstanceState is called after onStart method. I think (by the way, i'm not sure which tutorial you are talking about, i'm just thinking loudly) when saying "In the Android Fragments Tutorial it clearly states that "Once the activity reaches the resumed state, you can freely add and remove fragments to the activity." you can be sure that activity's state is restored. So you can add / remove your fragments but adding or removing fragments in onResume can cause some other issues.

What i suggest is simply check if savedInstanceState null and than do your operations in activity's onCreate method. With this approach we check if there's a previous state to be restored. Do not commit fragment transactions after onStop called which can cause IllegalStateException (For example in result of long running background tasks such as async tasks.)

Here you can see an official example from here: https://developer.android.com/training/basics/fragments/fragment-ui.html

savepopulation
  • 11,736
  • 4
  • 55
  • 80
  • So in the end you suggest to place the fragment's transactions inside the onCreate, after checking if savedInstanceState is null. That seems quite right because also the official android tutorials place the transaction inside the onCreate method. My questions is why they state that we can add or remove fragments "once the activity r e a c h e s the resumed state". That is what I dont understand. Because to my eyes onCreate is does not mean that the activity has reached its resumed state, right? – Mr T Nov 18 '16 at 13:17
  • i tried to explain in my answer. when you reach to onResume you can say that "my activity's state completely restored." so you can avoid sate loss and avoid illegal state exception. – savepopulation Nov 18 '16 at 13:27
-1

when u add fragment onCreate stage,android is creating your activity and when its done it would save that state at super.onCreate(savedInstanceState); .So initially fragments are not supposed to be a part of the activity as they are variable.Hence you should ideally add fragment after onCreate stage. You can add fragment at onCreate stage also,but sometime it would result in failure of saving of state or leaked window issue,hence avoid it

Ak9637
  • 990
  • 6
  • 12
  • So if not onCreate where to commit a fragment's transactions? Maybe onResume? Where exactly? – Mr T Nov 18 '16 at 13:19
  • try to do it in onResume , make sure you are importing correct package also,import android.app.FragmentManager; import android.app.FragmentTransaction; – Ak9637 Nov 19 '16 at 08:07