【问题标题】: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在 Activity 内部,如何暂停 for 循环以调用片段,然后在按钮单击片段后恢复循环以重新开始
【发布时间】:2017-04-02 04:51:07
【问题描述】:

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

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

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

}

【问题讨论】:

  • 是的,您可以这样做,但在我们帮助您之前,您必须展示您已经尝试过的内容。
  • 我是 android 片段和活动通信的新手。无法想象循环将如何在 Activity 和 Fragment 之间暂停和继续。
  • 使用处理程序概念做到了,这是UI和后台进程之间最好的沟通方式

标签: android android-activity fragment


【解决方案1】:
    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();
                }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多