在 Activity 内部,如何暂停 for 循环以调用片段,然后在按钮单击片段后恢复循环以重新开始
Posted
技术标签:
【中文标题】在 Activity 内部,如何暂停 for 循环以调用片段,然后在按钮单击片段后恢复循环以重新开始【英文标题】: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 【发布时间】: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和后台进程之间最好的沟通方式 【参考方案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();
【讨论】:
以上是关于在 Activity 内部,如何暂停 for 循环以调用片段,然后在按钮单击片段后恢复循环以重新开始的主要内容,如果未能解决你的问题,请参考以下文章
For 循环:如何在 100 次循环后暂停 x 秒? [复制]
如何在DispatcherTimer结束之前暂停for循环?
在 Shiny 应用程序中,如何暂停 for 循环以获取用户输入,然后在单击按钮后继续?