Nandinee Sahoo Nandinee Sahoo - 12 days ago 5
Android Question

Inside Activity, How to pause a for loop to call a fragment and after button click on fragment then resume the loop to start again

Inside my activity how can I make my for loop to pause to call a fragment. Inside fragment on button click how to come back to activity and resume the loop to start the process again.

My piece of code inside the activity to start a fragment inside a loop

for(int i = 0; i < lQuestModels.size(); i++){
StartMyFragment start_survey=new StartMyFragment();
Bundle bundle = new Bundle();
bundle.putString("survey_id", "111");
bundle.putString("survey_name", "testing");
bundle.putString("survey_desc", "Yes done");
bundle.putString("survey_no_of_question", "5");
bundle.putString("jListString", lQuestModels.get(i).toString());
start_survey.setArguments(bundle);

fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack();
fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.welcomeSurveyLayout,start_survey);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

}

Answer
    I did in the following way.

In my activity (Lets say SurveyProcessorActivity)-

public static Boolean isComplete = new Boolean(true);
    private Handler mHandler ;
    for(int i = 0; i < lQuestModels.size(); i++){

    mHandler.post(new Runnable() {
                            @Override
                            public void run() {


                                StartSTSurveyFragment start_survey=new StartSTSurveyFragment();
                                Bundle bundle = new Bundle();
                                bundle.putSerializable("survey",survey);
                                bundle.putSerializable("currentQuestModel",currentQuestModel);
                                start_survey.setArguments(bundle);
                                fragmentManager = getSupportFragmentManager();
                                fragmentManager.popBackStack();
                                fragmentTransaction = fragmentManager.beginTransaction();
                                fragmentTransaction.replace(R.id.frame_processor,start_survey);
                                fragmentTransaction.addToBackStack("StartMASurveyFragment");
                                fragmentTransaction.commit();

                            }

                        });

    synchronized (isComplete) {
                    try {
                        System.out.println(TAG + "Hey I'm here waiting");
                        isComplete.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                System.out.println(TAG + "Hey I'm notified");
    }

In my fragment on button click-

 synchronized (SurveyProcessorActivity.isComplete) {
                    SurveyProcessorActivity.isComplete.notify();
                }
Comments