Fragment Transaction show() does not show fragment on second time - android

I have a fragment inside of a fragment. This setup works fine until you rotate the device. After you rotate the device the inner fragment does not show anymore.
I am calling the .show() method and I have a couple of log statements to display what's going on, and the fragment just doesn't show up after rotation.
Any idea on what I'm doing wrong?
Here's my code. The issue can be found in the private method displayTab():
public static class MyTabsListener<T extends Fragment> implements ActionBar.TabListener
{
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Fragment mFragment;
private FragmentManager fm;
private Fragment fragmentJobListDetails;
private Fragment fragmentTrends;
public MyTabsListener(Activity activity, String tag, Class<T> clz, FragmentManager fragmentManager)
{
this(activity, tag, clz, null,fragmentManager);
}
public MyTabsListener(Activity activity, String tag, Class<T> clz, Bundle args, FragmentManager fragmentManager)
{
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
fm = fragmentManager;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached())
{
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.hide(mFragment);
ft.commit();
}
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft)
{
displayTab(tab, ft);
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft)
{
if (mFragment != null)
{
ft.hide(mFragment);
//ft.remove(mFragment);
}
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft)
{
displayTab(tab, ft);
}
private void displayTab(Tab tab, FragmentTransaction ft)
{
fragmentJobListDetails = mActivity.getFragmentManager().findFragmentByTag("jobDetails");
fragmentTrends = mActivity.getFragmentManager().findFragmentByTag("trends");
if (fragmentJobListDetails != null && !fragmentJobListDetails.isDetached())
{
ft.hide(fragmentJobListDetails);
}
if (mFragment == null)
{
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
}
else
{
if (mFragment == fragmentTrends)
{
System.out.println("fragments are equal");
System.out.println("equal. Fragment added? = " + mFragment.isAdded());
System.out.println("equal. Fragment detached? = " + mFragment.isDetached());
System.out.println("equal. Fragment visible? = " + mFragment.isVisible());
ft.show(fragmentTrends);
System.out.println("equal. Fragment added? = " + mFragment.isAdded());
System.out.println("equal. Fragment detached? = " + mFragment.isDetached());
System.out.println("equal. Fragment visible? = " + mFragment.isVisible());
}
ft.show(mFragment);
}
}
}// end MyTabsListener
And here's the output of my logs, after rotation:
11-25 09:17:24.895: I/System.out(14605): mFragment = TrendsPagerHolder{427a94a8 #1 id=0x1020002 trends}
11-25 09:17:24.895: I/System.out(14605): fragments are equal
11-25 09:17:24.895: I/System.out(14605): equal. Fragment added? = true
11-25 09:17:24.895: I/System.out(14605): equal. Fragment detached? = false
11-25 09:17:24.895: I/System.out(14605): equal. Fragment visible? = false
11-25 09:17:24.895: I/System.out(14605): equal. Fragment added? = true
11-25 09:17:24.895: I/System.out(14605): equal. Fragment detached? = false
11-25 09:17:24.895: I/System.out(14605): equal. Fragment visible? = false
Thanks!

I'm not sure what is wrong with the entire operation, but I use the following lines and it always works:
MyDialogFragment dialogFragment = new MyDialogFragment(data);
dialogFragment.show(getFragmentManager(), key);
Note that MyDialogFragment has two constructors one excepts the data the other is default (empty)
using the saveState and restoreState allow you to save the dialog state.
I hope this will work for you as well.

You need to override the default execution of configuration changes. You can do this by using the configChanges attribute of the activity tag in AndroidManifest. Read more about it here.

Related

Going back to fragment second time using tabs shows blank fragment

I have 2 tabs in my app, using tablistner and I'm facing an issue when I'm navigating in a very specific situation to other tab and then navigating back to the first tab.
It happens after i load a fragment called "setFrom" from another fragment:
public void LoadSetFrom ()
{
final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
SherlockFragment setFrag = new setFrom();
ft.replace(R.id.main_layout, setFrag, "setfrom");
ft.commit();
}
This "setFrom" fragment is one of my 2 tabs, after that I'm navigatin to the second tab and when I'm going back to "setFrom" the tabs navigation still appears but the fragment is totally blank.
I'm using TabListener that way:
public class TabListener<T extends SherlockFragment> implements com.actionbarsherlock.app.ActionBar.TabListener
{
private SherlockFragment mFragment;
private setFrom fromFragment;
private final SherlockFragmentActivity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(SherlockFragmentActivity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
SherlockFragment preInitializedFragment = (SherlockFragment)mActivity.getSupportFragmentManager().findFragmentByTag(mTag);
if (preInitializedFragment == null) {
mFragment = (SherlockFragment) SherlockFragment.instantiate(mActivity, mClass.getName());
ft.add(R.id.main_layout, mFragment, mTag);
}
else {
ft.attach(preInitializedFragment);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null)
ft.detach(mFragment);
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
After checking onTabSelected, "setFrom" is not null,it attached to the right fragment and it goes to ft.attach(preInitializedFragment) which is fine.
My question is why after the attach to the right fragment the view is still blank?
I was facing the same problem
Solved it by adding setRetainInstance(true); to my Fragment's onCreate

nested fragments at ActionBar switching

I use ActionBar and use inner TabListener in the Activity:
public static class TabListener<T extends Fragment>
implements ActionBar.TabListener{
private final Activity myActivity;
private final String myTag;
private final Class<T> myClass;
public TabListener(Activity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
// Check if the fragment is already initialized
if (myFragment == null) {
// If not, instantiate and add it to the activity
myFragment = Fragment.instantiate(myActivity, myClass.getName());
myActivity.setTitle( myClass.getName());
ft.add(android.R.id.content, myFragment, myTag);
} else {
// If it exists, simply attach it in order to show it
myActivity.setTitle( myFragment.getClass().getName());
ft.attach(myFragment);
}
Log.i("current fragment", myFragment.getClass().toString());
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (myFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(myFragment);
}
}
}
I need to switch another Fragment at the current tab from DialogFragment . I do it in common way like this:
Fragment newFragment = new ContactsArchiveFragment();
fragmentTransaction.replace(android.R.id.content, newFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
But when I switch to another tab, my current Fragment(which was switched to) is not detached and displays above. How can I solve this?
Try This..
public void addFragment(Fragment fragment, boolean addToBackStack,
int transition) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(android.R.id.content, fragment);
ft.setTransition(transition);
if (addToBackStack)
ft.addToBackStack(null);
ft.commit();
}
You can use true first time, then change it to false
addFragment(newfragment, true, FragmentTransaction.TRANSIT_NONE);
I solved my issue, using this. Here's the solution:
public static class TabListener<T extends Fragment>
implements ActionBar.TabListener{
private final Activity myActivity;
private final String myTag;
private final Class<T> myClass;
private Fragment mFragment;
public TabListener(Activity activity, String tag, Class<T> cls) {
myActivity = activity;
myTag = tag;
myClass = cls;
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
// Fragment myFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
Fragment preInitializedFragment = (Fragment) myActivity.getFragmentManager().findFragmentByTag(myTag);
if (preInitializedFragment == null) {
mFragment = (Fragment) Fragment.instantiate(myActivity, myClass.getName());
ft.add(android.R.id.content, mFragment, myTag);
} else {
ft.attach(preInitializedFragment);
}
}
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
Fragment preInitializedFragment = myActivity.getFragmentManager().findFragmentByTag(myTag);
if (preInitializedFragment != null) {
ft.detach(preInitializedFragment);
} else if (mFragment != null) {
ft.detach(mFragment);
}
}
}

Replacing Fragment in tab, changes all tabs

EDIT
I edited the previous question, wich i solved. Thanks a lot for the replies.
I have a gui with 3 tabs (News, Strategy and History), in one of them (News) i load an ExpandableListView and when the user clicks in one of the items from the list, it loads another fragment containg details from the selected item. I managed to replace the fragment in that tab with another fragment, using this code:
CategoryTab categories = new CategoryTab();//Fragment 2
FragmentManager manager = activity.getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(this.getId(), categories, "News");
//transaction.replace(android.R.id.content, categories, "News");
transaction.addToBackStack(null);
transaction.commit();
When the seconds fragment loads in the News tab, all the other tabs shows that fragment. I'm using this listener to manage tab navigation
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(Activity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
} else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
My question is ¿why all the tabs shows the same fragment when the replacement is done? and ¿how can i simulate a back button with the icon provided in the action bar, to load again fragment 1 in News Tab?
Any help will be appreciated
PD: Sorry for my english
Make sure you call getActivity() in or after onActivityCreated, as before then it will return null.
The Fragment will be attached to a null activity until onActivityCreated... that is, if you call getActivity() in onCreate or onCreateView, it will return null because the Activity hasn't been created yet. So make sure you have all of your calls to getActivity() in or after onActivityCreated

Action Bar Tabs - Having two fragments (one being dynamic) in one tab

I am trying to create a tab that displays a list on the left hand fragment and a detailed fragment on the right. When a user clicks a list item, the right hand fragment should change to the appropriate one.
I am new to android so I used a tutorial and I know I need to do something with the tablistener: public static class TabListener implements ActionBar.TabListener
{
private final Activity mActivity;
private final String mTag;
private final Class mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(Activity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
ft.add(android.R.id.content, mFragment, mTag);
tabtag = mTag;
}
else {
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
//Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
Another good example of what I need would be something like the Gmail app, with a list on the left, detail on the right, and keeping the action bar (mine has tabs) on top.
I understand that this listener inflates a fragment for each tab clicked, but how do I inflate a layout that has two fragments in it?
Inflating a layout with two fragments is simple: you just specify the Class of each fragment in the layout. But you can't mix static (layout XML-defined) and dynamic Fragments. By which I mean, you can't dynamically instantiate a new Fragment in code and insert it into a View container (e.g., a LinearLayout) that was originally populated with Fragments in the XML.

Fragment onCreateView and onActivityCreated called twice

I'm developing an app using Android 4.0 ICS and fragments.
Consider this modified example from the ICS 4.0.3 (API level 15) API's demo example app:
public class FragmentTabs extends Activity {
private static final String TAG = FragmentTabs.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_TITLE);
bar.addTab(bar.newTab()
.setText("Simple")
.setTabListener(new TabListener<SimpleFragment>(
this, "mysimple", SimpleFragment.class)));
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
Log.d(TAG, "FragmentTabs.onCreate tab: " + savedInstanceState.getInt("tab"));
Log.d(TAG, "FragmentTabs.onCreate number: " + savedInstanceState.getInt("number"));
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
public static class TabListener<T extends Fragment> implements ActionBar.TabListener {
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
private final Bundle mArgs;
private Fragment mFragment;
public TabListener(Activity activity, String tag, Class<T> clz) {
this(activity, tag, clz, null);
}
public TabListener(Activity activity, String tag, Class<T> clz, Bundle args) {
mActivity = activity;
mTag = tag;
mClass = clz;
mArgs = args;
// Check to see if we already have a fragment for this tab, probably
// from a previously saved state. If so, deactivate it, because our
// initial state is that a tab isn't shown.
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
Log.d(TAG, "constructor: detaching fragment " + mTag);
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
Log.d(TAG, "onTabSelected adding fragment " + mTag);
ft.add(android.R.id.content, mFragment, mTag);
} else {
Log.d(TAG, "onTabSelected attaching fragment " + mTag);
ft.attach(mFragment);
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
Log.d(TAG, "onTabUnselected detaching fragment " + mTag);
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(mActivity, "Reselected!", Toast.LENGTH_SHORT).show();
}
}
public static class SimpleFragment extends Fragment {
TextView textView;
int mNum;
/**
* When creating, retrieve this instance's number from its arguments.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(FragmentTabs.TAG, "onCreate " + (savedInstanceState != null ? ("state " + savedInstanceState.getInt("number")) : "no state"));
if(savedInstanceState != null) {
mNum = savedInstanceState.getInt("number");
} else {
mNum = 25;
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
Log.d(TAG, "onActivityCreated");
if(savedInstanceState != null) {
Log.d(TAG, "saved variable number: " + savedInstanceState.getInt("number"));
}
super.onActivityCreated(savedInstanceState);
}
#Override
public void onSaveInstanceState(Bundle outState) {
Log.d(TAG, "onSaveInstanceState saving: " + mNum);
outState.putInt("number", mNum);
super.onSaveInstanceState(outState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Log.d(FragmentTabs.TAG, "onCreateView " + (savedInstanceState != null ? ("state: " + savedInstanceState.getInt("number")) : "no state"));
textView = new TextView(getActivity());
textView.setText("Hello world: " + mNum);
textView.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.gallery_thumb));
return textView;
}
}
}
Here is the output retrieved from running this example and then rotating the phone:
06-11 11:31:42.559: D/FragmentTabs(10726): onTabSelected adding fragment mysimple
06-11 11:31:42.559: D/FragmentTabs(10726): onCreate no state
06-11 11:31:42.559: D/FragmentTabs(10726): onCreateView no state
06-11 11:31:42.567: D/FragmentTabs(10726): onActivityCreated
06-11 11:31:45.286: D/FragmentTabs(10726): onSaveInstanceState saving: 25
06-11 11:31:45.325: D/FragmentTabs(10726): onCreate state 25
06-11 11:31:45.340: D/FragmentTabs(10726): constructor: detaching fragment mysimple
06-11 11:31:45.340: D/FragmentTabs(10726): onTabSelected attaching fragment mysimple
06-11 11:31:45.348: D/FragmentTabs(10726): FragmentTabs.onCreate tab: 0
06-11 11:31:45.348: D/FragmentTabs(10726): FragmentTabs.onCreate number: 0
06-11 11:31:45.348: D/FragmentTabs(10726): onCreateView state: 25
06-11 11:31:45.348: D/FragmentTabs(10726): onActivityCreated
06-11 11:31:45.348: D/FragmentTabs(10726): saved variable number: 25
06-11 11:31:45.348: D/FragmentTabs(10726): onCreateView no state
06-11 11:31:45.348: D/FragmentTabs(10726): onActivityCreated
My question is, why is the onCreateView and onActivityCreated called twice? The first time with a Bundle with the saved state and the second time with a null savedInstanceState?
This is causing problems with retaining the state of the fragment on rotation.
I was scratching my head about this for a while too, and since Dave's explanation is a little hard to understand I'll post my (apparently working) code:
private class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private Activity mActivity;
private final String mTag;
private final Class<T> mClass;
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
mFragment=mActivity.getFragmentManager().findFragmentByTag(mTag);
}
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.replace(android.R.id.content, mFragment, mTag);
} else {
if (mFragment.isDetached()) {
ft.attach(mFragment);
}
}
}
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
ft.detach(mFragment);
}
}
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
}
As you can see it's pretty much like the Android sample, apart from not detaching in the constructor, and using replace instead of add.
After much headscratching and trial-and-error I found that finding the fragment in the constructor seems to make the double onCreateView problem magically go away (I assume it just ends up being null for onTabSelected when called through the ActionBar.setSelectedNavigationItem() path when saving/restoring state).
I have had the same problem with a simple Activity carrying only one fragment (which would get replaced sometimes). I then realized I use onSaveInstanceState only in the fragment (and onCreateView to check for savedInstanceState), not in the activity.
On device turn the activity containing the fragments gets restarted and onCreated is called. There I did attach the required fragment (which is correct on the first start).
On the device turn Android first re-created the fragment that was visible and then called onCreate of the containing activity where my fragment was attached, thus replacing the original visible one.
To avoid that I simply changed my activity to check for savedInstanceState:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
/**making sure you are not attaching the fragments again as they have
been
*already added
**/
return;
}
else{
// following code to attach fragment initially
}
}
I did not even Overwrite onSaveInstanceState of the activity.
Ok, Here's what I found out.
What I didn't understand is that all fragments that are attached to an activity when a config change happens (phone rotates) are recreated and added back to the activity. (which makes sense)
What was happening in the TabListener constructor was the tab was detached if it was found and attached to the activity. See below:
mFragment = mActivity.getFragmentManager().findFragmentByTag(mTag);
if (mFragment != null && !mFragment.isDetached()) {
Log.d(TAG, "constructor: detaching fragment " + mTag);
FragmentTransaction ft = mActivity.getFragmentManager().beginTransaction();
ft.detach(mFragment);
ft.commit();
}
Later in the activity onCreate the previously selected tab was selected from the saved instance state. See below:
if (savedInstanceState != null) {
bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
Log.d(TAG, "FragmentTabs.onCreate tab: " + savedInstanceState.getInt("tab"));
Log.d(TAG, "FragmentTabs.onCreate number: " + savedInstanceState.getInt("number"));
}
When the tab was selected it would be reattached in the onTabSelected callback.
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
Log.d(TAG, "onTabSelected adding fragment " + mTag);
ft.add(android.R.id.content, mFragment, mTag);
} else {
Log.d(TAG, "onTabSelected attaching fragment " + mTag);
ft.attach(mFragment);
}
}
The fragment being attached is the second call to the onCreateView and onActivityCreated methods. (The first being when the system is recreating the acitivity and all attached fragments) The first time the onSavedInstanceState Bundle would have saved data but not the second time.
The solution is to not detach the fragment in the TabListener constructor, just leave it attached. (You still need to find it in the FragmentManager by it's tag) Also, in the onTabSelected method I check to see if the fragment is detached before I attach it. Something like this:
public void onTabSelected(Tab tab, FragmentTransaction ft) {
if (mFragment == null) {
mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
Log.d(TAG, "onTabSelected adding fragment " + mTag);
ft.add(android.R.id.content, mFragment, mTag);
} else {
if(mFragment.isDetached()) {
Log.d(TAG, "onTabSelected attaching fragment " + mTag);
ft.attach(mFragment);
} else {
Log.d(TAG, "onTabSelected fragment already attached " + mTag);
}
}
}
The two upvoted answers here show solutions for an Activity with navigation mode NAVIGATION_MODE_TABS, but I had the same issue with a NAVIGATION_MODE_LIST. It caused my Fragments to inexplicably lose their state when the screen orientation changed, which was really annoying. Thankfully, due to their helpful code I managed to figure it out.
Basically, when using a list navigation, onNavigationItemSelected() is automatically called when your activity is created/re-created, whether you like it or not. To prevent your Fragment's onCreateView() from being called twice, this initial automatic call to onNavigationItemSelected() should check whether the Fragment is already in existence inside your Activity. If it is, return immediately, because there is nothing to do; if it isn't, then simply construct the Fragment and add it to the Activity like you normally would. Performing this check prevents your Fragment from needlessly being created again, which is what causes onCreateView() to be called twice!
See my onNavigationItemSelected() implementation below.
public class MyActivity extends FragmentActivity implements ActionBar.OnNavigationListener
{
private static final String STATE_SELECTED_NAVIGATION_ITEM = "selected_navigation_item";
private boolean mIsUserInitiatedNavItemSelection;
// ... constructor code, etc.
#Override
public void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey(STATE_SELECTED_NAVIGATION_ITEM))
{
getActionBar().setSelectedNavigationItem(savedInstanceState.getInt(STATE_SELECTED_NAVIGATION_ITEM));
}
}
#Override
public void onSaveInstanceState(Bundle outState)
{
outState.putInt(STATE_SELECTED_NAVIGATION_ITEM, getActionBar().getSelectedNavigationIndex());
super.onSaveInstanceState(outState);
}
#Override
public boolean onNavigationItemSelected(int position, long id)
{
Fragment fragment;
switch (position)
{
// ... choose and construct fragment here
}
// is this the automatic (non-user initiated) call to onNavigationItemSelected()
// that occurs when the activity is created/re-created?
if (!mIsUserInitiatedNavItemSelection)
{
// all subsequent calls to onNavigationItemSelected() won't be automatic
mIsUserInitiatedNavItemSelection = true;
// has the same fragment already replaced the container and assumed its id?
Fragment existingFragment = getSupportFragmentManager().findFragmentById(R.id.container);
if (existingFragment != null && existingFragment.getClass().equals(fragment.getClass()))
{
return true; //nothing to do, because the fragment is already there
}
}
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
return true;
}
}
I borrowed inspiration for this solution from here.
It looks to me like it's because you are instantiating your TabListener every time... so the system is recreating your fragment from the savedInstanceState and then you are doing it again in your onCreate.
You should wrap that in a if(savedInstanceState == null) so it only fires if there is no savedInstanceState.

Categories

Resources