0

I'm creating a ViewPager with a dynamic number of Fragments, I provide the FragmentStatePagerAdapter of this ViewPager with a list of Fragments as in the following:

public class LightPageAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> myFragments;

    public LightPageAdapter(FragmentManager fragmentManager, List<Fragment> fragments) {
        super(fragmentManager);

        myFragments = fragments;

    }

   public Fragment getItem(int i) {

        return myFragments.get(i);

    }

   @Override
   public int getCount() {

        return myFragments.size();

    }
}

the fragments list is obtained when clicking some button on certain Fragment, So, i added the following method to this Fragment in order to adding this fragments list to the FragmentStatePagerAdapter:

public void fargmenting(List<Fragment> fragments){

   ViewPager view_pager = (ViewPager) MyFragment.view.findViewById(R.id.lighting_pager);
   view_pager.setAdapter(new LightPageAdapter(getChildFragmentManager(), fragments)); // this is where I'm getting the Exception

}

Hint: in my app I'm using VerticalViewPager not ViewPager but it's a library with the same behaviour of the ViewPager but with some modification that wouldn't affect the overall process.

And this is the Log Error:

05-07 11:51:02.404: E/AndroidRuntime(3174): FATAL EXCEPTION: main
05-07 11:51:02.404: E/AndroidRuntime(3174): java.lang.IllegalStateException: Activity has been destroyed
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:161)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.standards.VerticalViewPager.populate(VerticalViewPager.java:965)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.standards.VerticalViewPager.populate(VerticalViewPager.java:811)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.standards.VerticalViewPager.setAdapter(VerticalViewPager.java:334)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.fragments.MyFragment.fargmenting(Lighting_Right.java:92)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.isolace.Lighting.assign_vertical_gangs_swipping(Lighting.java:502)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.isolace.Lighting.select_room(Lighting.java:403)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.automation.isolace.Lighting$11.onClick(Lighting.java:351)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.view.View.performClick(View.java:4084)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.view.View$PerformClick.run(View.java:16966)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.os.Handler.handleCallback(Handler.java:615)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.os.Looper.loop(Looper.java:137)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-07 11:51:02.404: E/AndroidRuntime(3174):     at dalvik.system.NativeStart.main(Native Method)
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118

1 Answers1

1

After a lot of efforts I finally did find the solution:

The Activity was destroyed when calling setAdapter because it's creating a new Object of the adapter (as I assign the adapter as new object):

new LightPageAdapter(getChildFragmentManager(), fragments)

and that was destroying the old instance including the activity that it's initialization depended on this instance.

So that, I do split the fragmenting() method into two parts , the first part when calling it for the first time in order to make the first initialization, and the second part when only the data changed and no need to recreate a new instance of the FragmentStatePagerAdapter :

private static LightPageAdapter lpa;
private static PagerAdapter adapter;

public void fargmenting(List<Fragment> fragments, boolean first_init){

   if(first_init){
      ViewPager view_pager = (ViewPager) MyFragment.view.findViewById(R.id.lighting_pager);
      lpa = new LightPageAdapter(getChildFragmentManager(), fragments);
   }else{
      LightPageAdapter.myFragments = fragments; // here I'm setting the fragments manually instead of passing it into a new constructor
      lpa.notifyDataSetChanged();
   }
   adapter = lpa;
   view_pager.setAdapter(adapter);

}
Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • Could you delete your answer and let someone else do it? Unfortunately your English is too bad for the answer to be useful :(. Or if you can rewrite it please? – Radu Jun 01 '15 at 11:36
  • @Radu well, I see that the answer English is ok, the problem is that it's quite complicated, so, you can refer to the part that you don't understand and I'll clarify it for you, and to make it easier to understand I split the answer into points and paragraphs, may be that makes a little sense for you – Muhammed Refaat Jun 01 '15 at 11:48
  • @MuhammedRefaat could you clarify a bit where are you using this method and why is ti necessary? I think this may be the solution for my problem but I cannot understand it. – kike Sep 22 '15 at 13:56
  • 1
    @kike I'm using this method in all cases of fragmenting, but the difference is the flag I'm passing in the method called `first_init`, when this is the first init of the activity, I'm passing it with true to create the fragments, but when needing to just replace the fragments with another ones, I'm calling the method with false, so this flag is needed to be true only in the first time, and all the changes to the fragments later must have to be done with this flag = false – Muhammed Refaat Sep 22 '15 at 14:37