0

I have an odd situation here. I have a FragmentActivity which have a TabHost with 5 Tabs. In two of those Tabs I have ViewPager. say those Tabs are "Tab-A" and "Tab-B", now Tab-A has 3 Fragments inside its ViewPager and Tab-B has 2. All those Fragments are different. I am using my custom FragmentPagerAdapter to populate the ViewPagers. The weird thing is when I switch to another tab from Tab-A/Tab-B and come back to it the ViewPager doesn't show the Fragments anymore or it shows one fragment or two but not the 3.Even more, sometimes the Fragments(not all) from Tab-A's ViewPager shows up into Tab-B's ViewPager even though they use 2 different adapters.

By searching SO what I came to understand is that this might be due to the nesting of Fragments(as ViewPager is in a Fragment and its pages are also Fragments). I have tried passing the FragmentPagerAdapter getChildFragmentManager() instead of getFragmentManager(). But in that case when I switch to another tab and come back to Tab-A/Tab-B the app crashes(see log below).

I am Pasting my code below. Layout for the FragmentActivity

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout 
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<TabHost
    android:id="@android:id/tabhost" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="0"/>

    <FrameLayout
        android:id="@+android:id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"/>

        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:layout_weight="0"  />
    </LinearLayout>
</TabHost> 
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:background="@android:color/darker_gray"/>
</android.support.v4.widget.DrawerLayout>

in the activity I am maintaining a separate stack of Fragments for every tab.This is how fragments are added or shown on tab switch

/*Comes here when user switch tab, or we do programmatically*/
TabHost.OnTabChangeListener listener    =   new TabHost.OnTabChangeListener() {
  public void onTabChanged(String tabId) {
    /*Set current tab..*/
    mCurrentTab =   tabId;

    if(mStacks.get(tabId).size() == 0){
      /*
       *    First time this tab is selected. So add first fragment of that tab.
       *    Dont need animation, so that argument is false.
       *    We are adding a new fragment which is not present in stack. So add to stack is true.
       */
      if(tabId.equals(TABTAGS.MessageTab))
      {
          FragmentInbox_ MessageFragment=new FragmentInbox_();
          pushFragments(tabId, MessageFragment, false,true);
      }
      else if(tabId.equals(TABTAGS.VIPTab))
      {
          VIPOffers_ VIPTab=new VIPOffers_();
          Bundle b=new Bundle();
          b.putInt("mode", 1);
          VIPTab.setArguments(b);
          pushFragments(tabId, VIPTab, false,true);
      }
      else if(tabId.equals(TABTAGS.DatingTab))
      {
          FragmentDating_ datingTab=new FragmentDating_();

          pushFragments(tabId, datingTab, false,true);
      }
      else if(tabId.equals(TABTAGS.VenueTab))
      {
        Fragment_EventPager_ venueTab=new Fragment_EventPager_();
        pushFragments(tabId, venueTab, false,true);
      }
    }
    else 
    {
      /*
       *    We are switching tabs, and target tab is already has atleast one fragment. 
       *    No need of animation, no need of stack pushing. Just show the target fragment
       */
      pushFragments(tabId, mStacks.get(tabId).lastElement(), false,false);
    }
  }
};

This is how new Fragments are added in tab or removed from

public void pushFragments(String tag, Fragment fragment,boolean shouldAnimate, boolean shouldAdd){
  if(shouldAdd)
      mStacks.get(tag).push(fragment);
  FragmentManager   manager         =   getSupportFragmentManager();
  FragmentTransaction ft            =   manager.beginTransaction();
  if(shouldAnimate)
      ft.setCustomAnimations(R.anim.slide_in_right, R.anim.slide_out_left);
  ft.replace(R.id.realtabcontent, fragment);
  ft.commit();
}


public void popFragments(){
  /*    
   *    Select the second last fragment in current tab's stack.. 
   *    which will be shown after the fragment transaction given below 
   */
  Fragment fragment =   mStacks.get(mCurrentTab).elementAt(mStacks.get(mCurrentTab).size() - 2);

  /*pop current fragment from stack.. */
  mStacks.get(mCurrentTab).pop();

  /* We have the target fragment in hand.. Just show it.. Show a standard navigation animation*/
  FragmentManager   manager         =   getSupportFragmentManager();
  FragmentTransaction ft            =   manager.beginTransaction();
  ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
  ft.replace(R.id.realtabcontent, fragment);
  ft.commit();
}   


@Override
public void onBackPressed() 
{
    if(mStacks.get(mCurrentTab).size() == 1){
        super.onBackPressed();  // or call finish..
    }else{
        popFragments();
    }
}

Here is the layout of the Fragment that has the ViewPager

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profile_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_header"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="@color/bar_back"
    android:textColor="@android:color/holo_blue_light" />

</android.support.v4.view.ViewPager>

This is how the adapter to ViewPager is set.(I used Android Annotation library)

@AfterViews
void InitiateViews()
{
    getActivity().getActionBar().setDisplayShowCustomEnabled(false);
    getActivity().getActionBar().setTitle("Dating");
    getActivity().getActionBar().setIcon(R.drawable.nav_dating);
    setHasOptionsMenu(true);

    Pager.setOffscreenPageLimit(2);
    SearchPagerAdapter adapter=new SearchPagerAdapter(getFragmentManager());
    Pager.setAdapter(adapter);
}

And finally the FragmentPagerAdapter

public class SearchPagerAdapter extends FragmentPagerAdapter{

ArrayList<Fragment>  Fragments;
final int PAGE_COUNT=3;
public SearchPagerAdapter(FragmentManager fm) 
{
    super(fm);
    // TODO Auto-generated constructor stub
    Fragments=new ArrayList<Fragment>();
    Fragments.add(new FragmentBasicSearch_());
    Fragments.add(new FragmentSearchSealed_());
    Fragments.add(new Fragment_Username_Search_());
}

@Override
public Fragment getItem(int pos) {
    // TODO Auto-generated method stub
    if(Fragments==null)
    {
        Fragments=new ArrayList<Fragment>();
        Fragments.add(new FragmentBasicSearch_());
        Fragments.add(new FragmentSearchSealed_());
        Fragments.add(new Fragment_Username_Search_());

    }

    if(pos<PAGE_COUNT)
    {
        Log.d("PF", "get pager fragment");
        return Fragments.get(pos);
    }

    return null;
}

@Override
public CharSequence getPageTitle(int position) {
    // TODO Auto-generated method stub
    switch (position)
    {
        case 0:
            return "BASIC SEARCH";

        case 1:
            return "SEALED SECTION SEARCH";

        case 2:
            return "UERNAME SEARCH";

    }
    return super.getPageTitle(position);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return PAGE_COUNT;
}

}

And below is the Logcat output I got by passing the FragmentPagerAdapter ChildFragmentManager

10-01 17:16:33.103: E/AndroidRuntime(3093): FATAL EXCEPTION: main
10-01 17:16:33.103: E/AndroidRuntime(3093): java.lang.IllegalStateException: No activity
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.os.Handler.handleCallback(Handler.java:605)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.os.Looper.loop(Looper.java:137)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at android.app.ActivityThread.main(ActivityThread.java:4441)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at java.lang.reflect.Method.invokeNative(Native Method)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at java.lang.reflect.Method.invoke(Method.java:511)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:565)
10-01 17:16:33.103: E/AndroidRuntime(3093):     at dalvik.system.NativeStart.main(Native Method)

Any help or suggestion will be highly appreciated.

Mehedi
  • 394
  • 1
  • 3
  • 19
  • Have you check the below links.. http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed http://stackoverflow.com/questions/15784155/java-lang-illegalstateexception-no-activity http://stackoverflow.com/questions/15498216/getting-a-java-lang-illegalstateexception-no-activity-when-resuming-an-activity – Hariharan Oct 01 '13 at 16:10
  • I have checked 2 of them before, checking the 3rd one now. – Mehedi Oct 01 '13 at 17:12
  • http://stackoverflow.com/questions/15207305/getting-the-error-java-lang-illegalstateexception-activity-has-been-destroyed – Mehedi Oct 02 '13 at 11:29

0 Answers0