-1

There is one question.

So in my adpater class, I excute codes below.

DrinkFragment fragment3 = new DrinkFragment();
Bundle bundle3 = new Bundle();
bundle3.putInt("type",2);
fragment3.setArguments(bundle3);
return fragment3;

So I make an instance of DrinkFragment.
To excute getArguments() in DrinkFragment.java at onCreateView(~~~), onCreateView(~~~) must be excuted after fragment3.setArgument(bundle3) right?

So I don't think onCreateView(~~) is excuted the moment i create DrinkFragment instance. Then when does it excuted?

Thanks in advance. ;)

this is my PagerAdapter.java where I create Fragment instance.

package com.junga.project1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.Log;

public class PagerAdapter extends FragmentStatePagerAdapter {

    int numOfFragment;
    private static final String TAG = "PagerAdapter";

    public PagerAdapter(FragmentManager fm,int numOfFragment) {
        super(fm);
        this.numOfFragment = numOfFragment;
    }

    @Override
    public Fragment getItem(int i) {

        switch(i){
            case 0:
                DrinkFragment fragment1 = new DrinkFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("type",0);
                fragment1.setArguments(bundle);
                Log.d(TAG, "getItem: 0 ");
                Log.d(TAG, "Set the argument fragment1");
                return fragment1;
            case 1:
                DrinkFragment fragment2 = new DrinkFragment();
                Bundle bundle2 = new Bundle();
                bundle2.putInt("type",1);
                fragment2.setArguments(bundle2);
                Log.d(TAG, "Set the argument fragment2");
                Log.d(TAG, "getItem: 1");
                return fragment2;

            case 2:
                DrinkFragment fragment3 = new DrinkFragment();
                Bundle bundle3 = new Bundle();
                bundle3.putInt("type",2);
                fragment3.setArguments(bundle3);
                Log.d(TAG, "getItem: 2");
                Log.d(TAG, "getItem: Set the argument fragment3");
                return fragment3;

            default:
                return null;
        }


    }

    @Override
    public int getCount() {
        return numOfFragment;
    }
}

This is fragment.java where onCreateView() exists.


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class DrinkFragment extends Fragment {

    private static final String TAG = "DrinkFragment";
    int fragmentType;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d(TAG, "onCreateView: Created");
        Bundle bundle = this.getArguments();


        fragmentType = bundle.getInt("type",0);
        Log.d(TAG, "Got the bundle type : "+fragmentType);
        View view =  inflater.inflate(R.layout.fragment_drink,container,false);
        ImageView image = (ImageView) view.findViewById(R.id.image);

        switch(fragmentType){
            case 0 :
                image.setImageResource(R.drawable.fragment_soju);
                return view;
            case 1:
                image.setImageResource(R.drawable.fragment_makgeoli);
                return view;
            case 2:
                image.setImageResource(R.drawable.fragment_cheongju);
                return view;
            default:
                return view;

        }
    }
}```
Nick Bapu
  • 463
  • 1
  • 4
  • 14
  • call `setArguments()` in the fragment's `onCreate()` - but regardless, what do the logs say about the obtained `type` argument? – Shark May 30 '19 at 09:59
  • 1
    Fragment's Lifecycle will starts once its attach to a container . Did you set this pager Adapter to `ViewPager`? – ADM May 30 '19 at 10:03
  • @Shark well, about the type argument? well, i didn't printed out the ```type``` log, but the bundle works fine. – JoyfulJoyce May 30 '19 at 10:52
  • @ADM Yes, I set this pagerAdapter to ```ViewPager``` at my MainActivity and it works fine. I was asking this out of curiosity not because it doesn't work. Can you give me more details about Fragment's lifecycle will starts once its attach to a container? – JoyfulJoyce May 30 '19 at 10:54
  • 1
    You are asking about 3rd case isn't it ? For `fragment3` .. If yes:- Pager default OffsetLimit is 2 initially (One page to each side) so it will only get called for first two position first . after you swipe page to right it will get called for 3rd postilion. See [#setOffscreenPageLimit()](https://developer.android.com/reference/android/support/v4/view/ViewPager#setoffscreenpagelimit) and [This thread](https://stackoverflow.com/questions/11650152/viewpager-offscreen-page-limit). – ADM May 30 '19 at 11:00
  • @ADM First of all, thanks for answering me :) What i wanted to ask was not about fragment3. I was wondering when exactly fragment lifecycle starts. Does it start when the moment fragment instantiated? Or when it is attached to a container? Since I checked your reply, adapter attatch 2 fragments at a time. And at that moment (when apdater attatch 2 fragments), fragment's lifecycle starts? Am I understanding right? – JoyfulJoyce May 30 '19 at 11:06
  • Yeah thats right .. – ADM May 30 '19 at 11:08

1 Answers1

0

Sorry if my code is not in Java but what I did to pass arguments in kotlin was to make oncreateview to return the view first then call the function to onviewcreated so it will execute once the view has been created. Here is the code in Kotlin:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
                    val menufragment: Fragment = YourFragment()
                    val args = Bundle()
                    args.putString("str1", text) //text and text1 are strings
                    args.putString("str2", text1)
                menufragment.arguments = args
                    val fr = fragmentManager?.beginTransaction()
                    fr?.replace(R.id.content_frame, menufragment)
                    fr?.addToBackStack(null)
                    fr?.commit()
}
CoderUni
  • 5,474
  • 7
  • 26
  • 58
  • Even this was not the point of my question , thanks for the sharing and spending your valuable time on my question !! Have a wonderful day! @TechMaxed – JoyfulJoyce May 30 '19 at 11:11
  • Thank you :) I hope this will be able to help you in the future if ever you encounter this problem – CoderUni May 30 '19 at 11:32