在Activity活动中,如何暂停for循环来调用片段,然后在按钮上单击片段然后恢复循环以重新开始

时间:2016-11-18 10:57:51

标签: android android-activity fragment

在我的活动中,如何让for循环暂停以调用片段。按钮内的片段单击如何返回活动并恢复循环以再次启动该过程。

我在活动中创建一段代码来启动循环中的片段

<com.android.systemui.statusbar.phone.StatusBarWindowView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:systemui="http://schemas.android.com/apk/res/com.android.systemui"
    android:focusable="true"
    android:descendantFocusability="afterDescendants"
    android:fitsSystemWindows="true"
    android:background="#00000000"
    style="@android:style/Theme.Translucent" >

1 个答案:

答案 0 :(得分:0)

    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();
                }