在视图寻呼机中使用片段时,不会调用片段中的onCreate方法

时间:2016-07-18 01:26:18

标签: android android-fragments android-viewpager tabview

这是我的活动类,使用viewpager和tablayout

在oncreate方法中调用片段
    public class testScreen2 extends AppCompatActivity implements      View.OnClickListener {
    MyPageAdapter pageAdapter;

    private ProgressDialog pDialog;

    private static final String TAG_SUCCESS = "success";

    private static final String TAG_MESSAGE = "message";

    TextView mTextField;

    TimerTask  timerTask;

    Timer timer;

    int numberSubmitted=0;

    int s_id =20;


    ImageView ivSubmit;
    TextView tv;
    ViewPager pager;
    String jsonData;
    static ArrayList<ArrayList<Integer>> options_answer = new ArrayList<ArrayList<Integer>>();
    static ArrayList<ArrayList<Integer>> options_answer_submitted = new ArrayList<ArrayList<Integer>>();
    Button button;
    JSONParser jsonParser = new JSONParser();
    MyPageAdapter adapter;
    int[] type = {2, 3, 4, 9, 2};
    int size;
    static ArrayList<Integer> OPTION_COUNT_AGAIN= new ArrayList<Integer>();
static ArrayList<Integer> ID_LIST= new ArrayList<Integer>();
static ArrayList<Integer> TYPE= new ArrayList<Integer>();
static ArrayList<Integer> SUBQUESTION_OPTION_COUNT= new ArrayList<Integer>();
static ArrayList<Integer> mcq_id= new ArrayList<Integer>();
static ArrayList<Integer> section_question_id= new ArrayList<Integer>();
static ArrayList<Integer> yn_answer= new ArrayList<Integer>();
static ArrayList<Integer> yn_id= new ArrayList<Integer>();
static ArrayList<Integer> tf_id= new ArrayList<Integer>();
static ArrayList<Integer> tf_answer= new ArrayList<Integer>();
static ArrayList<Integer> comp_ques_id= new ArrayList<Integer>();
static ArrayList<Integer> subques_tf_ans= new ArrayList<Integer>();
static ArrayList<Integer> subques_yn_ans= new ArrayList<Integer>();
static ArrayList<Integer> comp_subques_id= new ArrayList<Integer>();;

    static ArrayList<String> mcq= new ArrayList<String>();
static ArrayList<String> tf= new ArrayList<String>();
static ArrayList<String> question= new ArrayList<String>();
static ArrayList<String> yn,comp_ques_head= new ArrayList<String>();
static ArrayList<String>  comp_subques= new ArrayList<String>(); // comp_subquestion;

    static ArrayList<ArrayList<String>> options=new ArrayList<ArrayList<String>>();
static ArrayList<ArrayList<String>> subques_options=new ArrayList<ArrayList<String>>();;

    static ArrayList<ArrayList<Integer>> optionsId=new ArrayList<ArrayList<Integer>>();

    static ArrayList<ArrayList<Integer>> CompSubquesOptionsIdArrayListSelected = new ArrayList<ArrayList<Integer>>();


    static ArrayList<ArrayList<Integer>> subques_options_id=new ArrayList<ArrayList<Integer>>();;

    static ArrayList<ArrayList<Integer>> subques_options_answer = new ArrayList<ArrayList<Integer>>();

    static ArrayList<ArrayList<Integer>> optionsIdArrayListSelected = new ArrayList<ArrayList<Integer>>();

    static ArrayList<Integer> TYPE_SUB = new ArrayList<Integer>();
    static ArrayList<ArrayList<Integer>> optionsSubmitted = new ArrayList<ArrayList<Integer>>();
    static ArrayList<Integer> attempted = new ArrayList<Integer>();

    int Length;


    static ArrayList<String> MCQ_SELECTED_OPTIONS = new ArrayList<String>();

    static ArrayList<Integer> MCQ_ATTEMPT_PREVIOUS = new ArrayList<Integer>();

    static ArrayList<String> SUB_QUES_IN_SELECTED_OPTIONS= new ArrayList<String>();

    static ArrayList<Integer> SUB_QUES_IN_ATTEMPT_PREVIOUS = new ArrayList<Integer>();

    static ArrayList<Integer> TF_CORRECT= new ArrayList<Integer>();

    static ArrayList<Integer> YN_CORRECT = new ArrayList<Integer>();

    static ArrayList<Integer> TF_ATTEMPT_PREVIOUS = new ArrayList<Integer>();

    static ArrayList<Integer> YN_ATTEMPT_PREVIOUS = new ArrayList<Integer>();

    static ArrayList<Integer> COMP_TF_ATTEMPT_PREVIOUS = new ArrayList<Integer>();

    static ArrayList<Integer> COMP_TF_CORRECT = new ArrayList<Integer>();

static ArrayList<Integer> COMP_YN_ATTEMPT_PREVIOUS = new ArrayList<Integer>();

static ArrayList<Integer> COMP_YN_CORRECT = new ArrayList<Integer>();



@Override
    protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test_screen2);


            Log.d("Testing","In testscreen 2");
            ivSubmit=(ImageView)findViewById(R.id.imageView3);

        mTextField=(TextView)findViewById(R.id.counter);

            ivSubmit.setOnClickListener(this);
            button=(Button)findViewById(R.id.testscreen);
            button.setOnClickListener(this);
            Log.d("Debug", "Reached testscreen2");
            size = type.length;
            Intent intent = getIntent();

            if (options != null) {
                    Log.d("Debug", "Got arraylist options");
            }
            Length = intent.getIntExtra("Length", 0);

            TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);

            for (int i = 0; i < question.size(); i++)   {
                    int a = i + 1;
                    tabLayout.addTab(tabLayout.newTab().setText("" + a));
                Log.d("TYPE","TYPE is "+TYPE.get(i));
                Log.d("Debug","We have added tabnumber "+a);

            }



          tabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
            tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
            pager = (ViewPager) findViewById(R.id.viewpager);

            adapter = new MyPageAdapter(getSupportFragmentManager(), tabLayout.getTabCount(), TYPE, TYPE_SUB);

            pager.setOffscreenPageLimit(tabLayout.getTabCount() - 1);

            // the number of "off screen" pages to keep loaded each side of the current page



            pager.setAdapter(adapter);

            pager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
          tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {


                    pager.setCurrentItem(tab.getPosition());

                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

}}

现在我也发布了FragmentPagerAdapter类的代码

public class MyPageAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
String[] options;
int[] quesType;
questionOptionBoth tab1;
true_false tab2;
yes_no_fragment tab3;
comprehension_tf tab4;
comprehension_yn tab5;
comprehension_mcq tab6;
//fragment_comprehension tab4;
ArrayList<Integer> TYPE, TYPE_SUB;
public MyPageAdapter(FragmentManager fm, int NumOfTabs, ArrayList<Integer> TYPE, ArrayList<Integer> TYPE_SUB) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
    this.TYPE=TYPE;
    this.TYPE_SUB=TYPE_SUB;
}



@Override
public Fragment getItem(int position) {
    int type=TYPE.get(position);
    int type_sub=TYPE_SUB.get(position);
    if(type==2 && type_sub==0) {
        tab1 = questionOptionBoth.newInstance(position);
        Log.d("Fragment","We have created mcq fragment at position "+position );
        return tab1;
    }
    else if(type==3 && type_sub==0)
    {
        tab2 = true_false.newInstance(position);
        return tab2;
    }

    else
    {
        return null;
    }
}




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

}

现在,问题是片段的oncreate方法没有被调用,但片段正在被实例化。这在运行pre-lollipop版本的android的设备中运行良好,但在棒棒糖和上面一些片段只是实例化但是他们的生命周期方法如onCreate和onAttach没有被调用我也发布了片段代码供参考。

   public class questionOptionBoth extends Fragment implements      MyRecyclerViewAdapter.MyInterface {


ImageView ivQuestion;
private MyRecyclerViewAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
String ques;
// TODO: Rename and change types of parameters

private int tabNumber;
public static final String TAB_NUMBER = "TAB_NUMBER";
RecyclerView mRecyclerView;

ArrayList<String> optionsArrayList = new ArrayList<String>();

ArrayList<Integer> optionsIdArrayList = new ArrayList<Integer>();

ArrayList<Integer> optionsIdArrayListSelected = new ArrayList<Integer>();

ArrayList<Integer> optionsArrayListPrevSelected = new ArrayList<Integer>();

 LinearLayout linearQuestionOption;

String optionsMarked;


String[] optionPreviouslyMarked;


public static final questionOptionBoth newInstance(int tabNumber) {
   Log.d("DebugQO","We are in mcq fragment at tabnumber "+tabNumber);
    questionOptionBoth f = new questionOptionBoth();
    Bundle bdl = new Bundle();

    bdl.putInt(TAB_NUMBER, tabNumber);

    f.setArguments(bdl);






    return f;
}

@Override
public void onAttach(Context context) {
    Log.d("Attach","We are in onattach method");

    super.onAttach(context);
}

@Override
public void onAttach(Activity activity) {
    Log.d("Attach","We are in onattach method");
    super.onAttach(activity);
}

@Override
public void onPause() {
    Log.d("Pause","Onpause is called");
    super.onPause();
}

@Override
public void onCreate(Bundle savedInstanceState) {
   Log.d("Debug","We are in oncreate method at tabnumber "+tabNumber);
    super.onCreate(savedInstanceState);


    if (getArguments() != null) {
        tabNumber = getArguments().getInt(TAB_NUMBER);
        Log.d("TabNumber","Tabnumber is "+tabNumber);


        optionsArrayList = ((testScreen2) getActivity()).getOptionsFromQues(tabNumber);

        optionsIdArrayList = ((testScreen2) getActivity()).getOptionIdsFromQues(tabNumber);

        optionsMarked=((testScreen2)getActivity()).get_MCQ_SELECTED_OPTIONS(tabNumber);

        if(optionsMarked==null||optionsMarked.equals("null"))
        {} else{
            optionPreviouslyMarked= optionsMarked.split(",");
        }

        if (optionsArrayList != null) {
            Log.d("Debug", "Got options list in oncreate method in fragment");
        }

        if (optionsIdArrayList != null) {
            for (int i = 0; i < optionsIdArrayList.size(); i++) {
                optionsIdArrayListSelected.add(i, 0);
                optionsArrayListPrevSelected.add(i,0);
                Log.d("OptionIDArraylist","OPtionIDArrayList previously selected "+ i+" at tabnumber"+tabNumber+" is "+optionsArrayListPrevSelected.get(i));
            }



         if(optionsMarked==null||optionsMarked.equals("null"))
         {} else
         {
             Log.d("options marked", "options marked is not null");
             Log.d("options marked","options marked is "+optionsMarked);
             for(int z=0;z<optionPreviouslyMarked.length;z++)
             {


                    int position=optionsIdArrayList.indexOf(Integer.valueOf(optionPreviouslyMarked[z].trim()));


                 optionsIdArrayListSelected.set(position,Integer.valueOf(optionPreviouslyMarked[z].trim()));
                optionsArrayListPrevSelected.add(position, 1);
             }
         }




            ((testScreen2) getActivity()).CompSubquesOptionsIdArrayListSelected.add(tabNumber,null);

            ((testScreen2) getActivity()).optionsIdArrayListSelected.add(tabNumber, new ArrayList<Integer>(optionsIdArrayListSelected));

        }



        Log.d("Debug","The size in questionOptionBoth at tabNumber "+tabNumber+" is "+optionsIdArrayList.size());

        ques = ((testScreen2) getActivity()).getQues(tabNumber);

        // String[] quesParts = ques.split("#IM@Ge#B!n@RyD@T@#");
        // quesData = quesParts[0];
        // quesImage = quesParts[1];

        Log.d("Debug", "Got questions");
    }
    else
    {
        Log.d("tabbb","tab number is null");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    Log.d("DebugOncreateV","We are in oncreateview at tabnumber "+tabNumber);

    final View rootView = inflater.inflate(R.layout.fragment_question_option_both, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.option_recycler_view);
    Log.d("Debug", "Initialised recycler view");

    mRecyclerView.setHasFixedSize(true);

    Log.d("Debug", "Set the fixed size as true");

    mLayoutManager = new LinearLayoutManager(getContext());

    Log.d("Debug", "Reached layout manager");

    mRecyclerView.setLayoutManager(mLayoutManager);

    mAdapter = new MyRecyclerViewAdapter(optionsArrayList, optionsIdArrayList, this,getContext(),optionsArrayListPrevSelected);


    Log.d("Debug", "Initialised adapter");

    mRecyclerView.setAdapter(mAdapter);

    Log.d("Debug", "Set adapter");

    linearQuestionOption=(LinearLayout)rootView.findViewById(R.id.linearQuestionOption);







    return rootView;
}


public TextView addTextView()
{
    TextView tv= new TextView(getContext());
    // tv.setPadding(10, 0, 10, 0);
    FlowLayout.LayoutParams lp = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(lp);
    //tv.setTypeface(null, Typeface.BOLD);
    tv.setTextColor(Color.parseColor("#000000"));
    return tv;
}

public ImageView addImageView()
{
    ImageView iv= new ImageView(getContext());
    // iv.setPadding(10, 0, 10, 0);
    FlowLayout.LayoutParams lp = new FlowLayout.LayoutParams(FlowLayout.LayoutParams.WRAP_CONTENT, FlowLayout.LayoutParams.WRAP_CONTENT);
    iv.setLayoutParams(lp);
    return iv;
}


@Override
public void onClickCheckBox(int event, int position) {
    if (event == 1) {


        int id = optionsIdArrayList.get(position);

        ((testScreen2) getActivity()).optionsIdArrayListSelected.get(tabNumber).set(position, id);

        Toast.makeText(getContext(), "We have clicked " + position + " checkbox", Toast.LENGTH_SHORT).show();
    } else {
        ((testScreen2) getActivity()).optionsIdArrayListSelected.get(tabNumber).set(position, 0);
    }
}

0 个答案:

没有答案