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

Bundle args not working #68

Closed
Dwite opened this issue Aug 4, 2015 · 3 comments
Closed

Bundle args not working #68

Dwite opened this issue Aug 4, 2015 · 3 comments

Comments

@Dwite
Copy link

Dwite commented Aug 4, 2015

I'm using such fragment

public class WelcomeSlide extends Fragment {

    public static final String SLIDE_NAME = "slideName";

    @InjectView(R.id.welcome_slide_title)
    TextView mWelcomeSlideTitle;

    @InjectView(R.id.welcome_slide_image)
    ImageView mWelcomeSlideImage;

    @InjectView(R.id.welcome_slide_text)
    TextView mWelcomeSlideText;

    SlideName mSlideName;

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    public enum SlideName {
        AD,
        BADGE,
        BUZZER,
        CHAT,
        MEGAPHONE,
        NEWS
    }

    public WelcomeSlide() {}

    public static WelcomeSlide newInstance(SlideName slideName) {
        WelcomeSlide welcomeSlide = new WelcomeSlide();
        Bundle bundle = new Bundle();
        String slideNameString = slideName.name();
        Log.d("SLIDE", slideNameString);

        bundle.putString(SLIDE_NAME, slideName.name());
        welcomeSlide.mSlideName = slideName;
        welcomeSlide.setArguments(bundle);
        return welcomeSlide;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null && getArguments().containsKey(SLIDE_NAME)) {
            mSlideName = SlideName.valueOf(getArguments().getString(SLIDE_NAME, ""));
        } else {
            Log.d("SLIDE", "empty args");
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_slide_welcome, container, false);
        ButterKnife.inject(this, v);

        int drawable = R.drawable.ic_news;
        int width = getScreenWidth();
        String slideTitle = "";
        String slideText = "";
        v.setBackgroundResource(R.color.primary);

        switch (mSlideName) {
            case NEWS:
                slideTitle = getString(R.string.slide_news_title);
                slideText = getString(R.string.slide_news_text);
                drawable = R.drawable.ic_news;
                break;
            case AD:
                slideTitle = getString(R.string.slide_advertisements_title);
                slideText = getString(R.string.slide_advertisements_text);
                drawable = R.drawable.ic_ad;
                break;
            case CHAT:
                slideTitle = getString(R.string.slide_chat_title);
                slideText = getString(R.string.slide_chat_text);
                drawable = R.drawable.ic_chat;
                break;
            case MEGAPHONE:
                slideTitle = getString(R.string.slide_respublic_title);
                slideText = getString(R.string.slide_respublic_text);
                drawable = R.drawable.ic_megaphone;
                break;
            case BADGE:
                slideTitle = getString(R.string.slide_datings_title);
                slideText = getString(R.string.slide_datings_text);
                drawable = R.drawable.ic_badge;
                break;
            default:
                slideTitle = getString(R.string.slide_news_title);
                slideText = getString(R.string.slide_news_text);
                drawable = R.drawable.ic_news;
                break;
        }

        mWelcomeSlideTitle.setText(slideTitle);
        mWelcomeSlideText.setText(slideText);
        Picasso.with(getActivity())
                .load(drawable)
                .resize(0, (width / 3) * 2)
                .into(mWelcomeSlideImage);

        return v;
    }

    private int getScreenWidth() {
        Display display = getActivity().getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        return size.x;
    }
}

Creating intro class

public class WelcomeActivity extends AppIntro2 {

    public static final String IS_FROM_SEETINGS = "fromSettings";

    @Override
    public void init(Bundle bundle) {
        addSlide(WelcomeSlide.newInstance(WelcomeSlide.SlideName.NEWS), this);
        addSlide(WelcomeSlide.newInstance(WelcomeSlide.SlideName.CHAT), this);
        addSlide(WelcomeSlide.newInstance(WelcomeSlide.SlideName.MEGAPHONE), this);
        addSlide(WelcomeSlide.newInstance(WelcomeSlide.SlideName.AD), this);
        addSlide(WelcomeSlide.newInstance(WelcomeSlide.SlideName.BADGE), this);

    }

    @Override
    public void onDonePressed() {
        if (!getIntent().getBooleanExtra(IS_FROM_SEETINGS, false)) {
            startActivity(new Intent(this, AuthActivity.class));
        }
        this.finish();
    }
}

And looks like your addSlide in AppIntro2

public void addSlide(@NonNull Fragment fragment, @NonNull Context context) {
        this.fragments.add(Fragment.instantiate(context, fragment.getClass().getName()));
        this.mPagerAdapter.notifyDataSetChanged();
    }

is ignoring my fragment at all

@paolorotolo
Copy link
Member

Hi @Dwite,
which version of AppIntro are you using?

This issue was fixed in latest release. Now you can simply use:
addSlide(fragment);

AppIntro's addSlide method:

    public void addSlide(@NonNull Fragment fragment) {
        fragments.add(fragment);
        mPagerAdapter.notifyDataSetChanged();
    }

@Dwite
Copy link
Author

Dwite commented Aug 4, 2015

@paolorotolo I was using 2.0.0
upgrade to last version fixed issue, thanks.

@Dwite Dwite closed this as completed Aug 4, 2015
@paolorotolo
Copy link
Member

👍

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

No branches or pull requests

2 participants