Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IllegalStateException: Fragment already active #109

Closed
austinhodak opened this issue Aug 8, 2016 · 7 comments
Closed

IllegalStateException: Fragment already active #109

austinhodak opened this issue Aug 8, 2016 · 7 comments
Assignees
Labels
Milestone

Comments

@austinhodak
Copy link

austinhodak commented Aug 8, 2016

When attempting to add a new slide via a callback from a fragment slide, a IllegalStateException is thrown.

java.lang.IllegalStateException: Fragment already active
        at android.support.v4.app.Fragment.setInitialSavedState(Fragment.java:581)
        at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:114)
        at com.heinrichreimersoftware.materialintro.slide.SlideAdapter.instantiateItem(SlideAdapter.java:93)
        at com.heinrichreimersoftware.materialintro.view.FadeableViewPager$PagerAdapterWrapper.instantiateItem(FadeableViewPager.java:124)
        at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1003)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1151)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1085)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1611)
        at android.view.View.measure(View.java:18797)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:898)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:18797)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
        at android.view.View.measure(View.java:18797)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:18797)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at android.view.View.measure(View.java:18797)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
        at android.view.View.measure(View.java:18797)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2658)
        at android.view.View.measure(View.java:18797)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
        at android.view.Choreographer.doFrame(Choreographer.java:606)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5481)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
@heinrichreimer
Copy link
Owner

You can't add the same fragment twice. That's not possible in Android's Fragment framework.

Please make sure you instantiate each Fragment seperately.

Also try if calling notifyDataSetChanged() in your intro activity helps.

@austinhodak
Copy link
Author

austinhodak commented Aug 14, 2016

My current testing is only trying to add a slide from a button click and that is still crashing with the same error. See my code below.

package com.fireapps.firedepartmentmanager;

import ...;

import com.heinrichreimersoftware.materialintro.app.IntroActivity;
import com.heinrichreimersoftware.materialintro.slide.FragmentSlide;
import com.heinrichreimersoftware.materialintro.slide.SimpleSlide;
import com.heinrichreimersoftware.materialintro.slide.Slide;

public class FirstSignupActivity extends IntroActivity {

    private SimpleSlide loginChooserSlide;
    private SimpleSlide loginSlide;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addSlide(new SimpleSlide.Builder()
                .title("Welcome!")
                .description("Saving time and saving lives! \nThe BEST First Responder system available.")
                .background(R.color.md_teal_500)
                .backgroundDark(R.color.md_teal_700)
                .build());

        addSlide(new SimpleSlide.Builder()
                .title("Features!")
                .description("From responder tracking to incident reporting. Our system can do it all! Think we should add a feature? Let us know!")
                .background(R.color.md_cyan_500)
                .backgroundDark(R.color.md_cyan_700)
                .build());


        loginChooserSlide = new SimpleSlide.Builder()
                .background(R.color.md_green_500)
                .backgroundDark(R.color.md_green_700)
                .title("Create an Account or Login!")
                .buttonCtaLabel("Get Started!")
                .buttonCtaClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        new MaterialDialog.Builder(FirstSignupActivity.this)
                                .positiveText("YES")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        addSlide(loginSlide);
                                    }
                                })
                                .show();
                    }
                })
                .build();

        loginSlide = new SimpleSlide.Builder()
                .title("Login")
                .background(R.color.md_green_500)
                .backgroundDark(R.color.md_green_700)
                .build();

        addSlide(loginChooserSlide);

    }
}

@heinrichreimer
Copy link
Owner

You have to ensure that you can't click the material dialog twice. Otherwise the same slide (referencing the same fragment) will be added again.
Don't add the same slide twice. If you need to add slides dynamically you have to ensure that no duplicates are added.
As far as I understand your code it'll fail when a user opens the dialog the second time and klicks "YES", is that right?

@austinhodak
Copy link
Author

austinhodak commented Aug 15, 2016

At this point I can't even add a slide after the Intro is created. Throwing same error. Not adding the same slide, completely new slide.

public class FirstSignupActivity extends IntroActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        addSlide(new SimpleSlide.Builder()
                .title("Welcome!")
                .description("Saving time and saving lives! \nThe BEST First Responder system available.")
                .background(R.color.md_teal_500)
                .backgroundDark(R.color.md_teal_700)
                .build());

        addSlide(new SimpleSlide.Builder()
                .title("Features!")
                .description("From responder tracking to incident reporting. Our system can do it all! Think we should add a feature? Let us know!")
                .background(R.color.md_cyan_500)
                .backgroundDark(R.color.md_cyan_700)
                .build());



        addSlide(new SimpleSlide.Builder()
                .background(R.color.md_green_500)
                .backgroundDark(R.color.md_green_700)
                .title("Create an Account or Login!")
                .buttonCtaLabel("Get Started!")
                .buttonCtaClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        addSlide(new SimpleSlide.Builder()
                                .title("Login")
                                .background(R.color.md_green_500)
                                .backgroundDark(R.color.md_green_700)
                                .build());
                        notifyDataSetChanged();
                    }
                })
                .build());
    }
}`

@austinhodak
Copy link
Author

austinhodak commented Aug 15, 2016

Ok, so I've narrowed the issue down, if you provide a description it works and doesn't crash. If you don't give a description it crashes.

Edit: It's only crashing sometimes now, one time it'll work fine, restart and it crashes.

@austinhodak
Copy link
Author

Any progress? This is holding up my project finish.

@heinrichreimer
Copy link
Owner

Well, I checked the issue. As of my testing the issue only occurs when you add the slide twice. I would consider that normal behavior as your code should ensure to not let users add the same slide twice. I'll look into that again and I'll try to add some checks that prevent that internally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants