[Java for Android App Development] creambyemute:
Hey All,

I've fixed quite some bugs, annoyances and crashes in our Android App and so far it looks good but a new crash has appeared which didn't occur before (probably because in the past it crashed before it even reached that point).

I tried to reproduce this crash on multiple different phones (Nexus 5, Nexus 4, Xiaomi Mi4C, Samsung Galaxy S5, HTC One A9 and A8) by rotating, switching tabs, turning screen off/on and switching tabs and rotating and so on but I just couldn't reproduce it :(.

Stacktrace:

java.lang.IllegalStateException: No host
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1194)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1189)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:2001)
at android.support.v4.app.Fragment.performActivityCreated(SourceFile:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1051)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1207)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1189)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(SourceFile:2001)
at android.support.v4.app.Fragment.performActivityCreated(SourceFile:1976)
at android.support.v4.app.FragmentManagerImpl.moveToState(SourceFile:1051)
at android.support.v4.app.FragmentManagerImpl.attachFragment(SourceFile:1385)
at android.support.v4.app.BackStackRecord.run(SourceFile:728)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(SourceFile:1572)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(SourceFile:545)
at myPackageName.FragmentTabsActivity$TabManager.onTabChanged(SourceFile:183)
at android.widget.TabHost.invokeOnTabChangeListener(TabHost.java:468)
at android.widget.TabHost.setCurrentTab(TabHost.java:447)
at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:170)
at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:550)
at android.view.View.performClick(View.java:5106)
at android.view.View$PerformClick.run(View.java:20329)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5912)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

FragmentTabsActivity manifest entry:

<activity
android:name="myPackageName.FragmentTabsActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name"
android:launchMode="singleTask" >

<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

onTabChanged implementation from FragmentTabsActivity (I left a bit more than what is probably needed):

public static class TabManager implements TabHost.OnTabChangeListener {
private final FragmentActivity mActivity;
private final TabHost mTabHost;
private final int mContainerId;
private final Map<String, TabInfo> mTabs= new HashMap<String, TabInfo>();
TabInfo mLastTab;

static final class TabInfo {
private final String mTag;
private final Class<?> mClss;
private final Bundle mArgs;
private Fragment mFragment;

TabInfo(String tag, Class<?> clazz, Bundle args) {
mTag= tag;
mClss= clazz;
mArgs= args;
}
}

static class DummyTabFactory implements TabHost.TabContentFactory {
private final Context mContext;

public DummyTabFactory(Context context) {
mContext= context;
}

@Override
public View createTabContent(String tag) {
View v= new View(mContext);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}

public TabManager(FragmentActivity activity, TabHost tabHost, int containerId) {
mActivity= activity;
mTabHost= tabHost;
mContainerId= containerId;
mTabHost.setOnTabChangedListener(this);
}

public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
tabSpec.setContent(new DummyTabFactory(mActivity));
String tag= tabSpec.getTag();

TabInfo info= new TabInfo(tag, clss, 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.
info.mFragment= mActivity.getSupportFragmentManager().findFragmentByTag(tag);
if (info.mFragment != null && !info.mFragment.isDetached()) {
FragmentTransaction ft= mActivity.getSupportFragmentManager().beginTransaction();
ft.detach(info.mFragment);
ft.commitAllowingStateLoss();
}

mTabs.put(tag, info);
mTabHost.addTab(tabSpec);
}

@Override
public void onTabChanged(String tabId) {
TabInfo newTab= mTabs.get(tabId);
if (mLastTab != newTab) {
FragmentTransaction ft= mActivity.getSupportFragmentManager().beginTransaction();
if (mLastTab != null) {
if (mLastTab.mFragment != null) {
ft.detach(mLastTab.mFragment);
}
}
if (newTab != null) {
if (newTab.mFragment == null) {
newTab.mFragment= Fragment.instantiate(mActivity, newTab.mClss.getName(), newTab.mArgs);
ft.add(mContainerId, newTab.mFragment, newTab.mTag);
} else {
ft.attach(newTab.mFragment);
}
}

if (newTab.mFragment != null && newTab.mFragment instanceof MyMapFragment) {
MyMapFragment mapFragment= (MyMapFragment) newTab.mFragment;
mapFragment.clearBundleOnTabChange();
}
if (mLastTab != null && mLastTab.mFragment != null && mLastTab.mFragment instanceof MyMapFragment) {
MyMapFragment mapFragment= (MyMapFragment) mLastTab.mFragment;
mapFragment.clearBundleOnTabChange();
}

mLastTab= newTab;
ft.commitAllowingStateLoss();
mActivity.getSupportFragmentManager().executePendingTransactions();
}
}
}

FragmentTabsActivity is hosting 3 tabs where as every tab has one assigned Fragment.

If anyone knows how to reproduce/solve this I would be really happy about an answer. I've already found 2 or 3 possible solutions while googling but without being able to test which one actually solves my problem I feel uncomfortable :/

If needed and if it helps I can also add and describe the lifecycle of the FragmentTabsActivity and the hosted fragments