工作线程内的片段事务

时间:2014-03-15 08:07:36

标签: android multithreading android-fragments

基本上我尝试在后台线程中执行片段事务,并且在onSavedInstanceState之后我无法执行此操作。这就是我所做的:

我有两个片段的活动,片段A(包含UI所有元素)和片段B(包含进度条)。创建活动时,会添加片段A,以便用户看到UI元素(按钮和Edittexts)。然后,当在片段A中单击按钮时,(1)在父活动中启动后台任务,并且(2)片段A被片段B替换。所有这一切都正常。

但是当任务完成后,如果任务成功,我想开始一个新的活动,如果它失败了,我想用当前的片段替换当前的片段。我没有启动活动的问题,但是当我尝试进行片段事务时,应用程序崩溃说can't perform this action after onSavedInstanceState

我对此进行了大量研究并发现了很多有用的东西herehere,但我仍然无法解决我的问题。第一个链接是关于SO的问题,其中一个答案是将调用片段事务转移到活动的onResume方法,但我不知道如何做到这一点。此外,我尝试使用commitAllowingStateLoss,应用程序仍然崩溃,但logcat说Activity has been destroyed

注意:如果没有配置更改,一切都会顺利进行。

这是我的代码:

包含活动

    public class MainActivity extends FragmentActivity  {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.i("tag", "mainActivity onCreate");  
    Fragment taskf =getSupportFragmentManager().findFragmentByTag("taskfrag");

    //Put the Fragment A if Fragment B is not showing
    if(savedInstanceState == null){
    if(taskf == null){
        Log.i("tag", "MainFragment added"); 
        FragmentTransaction fragmentTransaction=   getSupportFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.wrapper, new MainFragment() ,   "mainfrag").commit();
    }
    }
}

//Fragment B is added here
public void addFragment(){
    Log.i("tag", "fragment added");
    this.getSupportFragmentManager().beginTransaction().replace(R.id.wrapper, new   TaskFragment(), "taskfrag").commit();       

}

// My dummy Task
public void startTask(){
    Log.i("tag", "start Task");
    new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
             for (int i = 0; i < 100; i++) {
                    Log.i("tag", "publishProgress(" + i + "%)");
                    SystemClock.sleep(100);
                  }

                        getSupportFragmentManager().beginTransaction().replace(R.id.wrapper, new MainFragment(),   "mainfrag").commitAllowingStateLoss();

        }


    }).start();

}

 }

片段A

    public class MainFragment extends Fragment {

View fView;
ProgressBar bar;
TextView textView;
Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i("tag", "onCreate in Main Fragment "); 
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.i("tag", "onCreate View in Mainfragment");  
    fView = inflater.inflate(R.layout.mainfragment, container, false);
    textView = (TextView) fView.findViewById(R.id.textview);

    button = (Button) fView.findViewById(R.id.button);

    //Button Click Listener 
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Log.i("tag", "button clicked"); 

            ((MainActivity)getActivity()).startTask();
            ((MainActivity)getActivity()).addFragment();

        }
    });
    return fView;
}

   }

片段B

    public class TaskFragment extends Fragment{

View fview;
ProgressBar bar;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i("tag", "TaskFragment Created");   
    super.onCreate(savedInstanceState);
    setRetainInstance(true);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    Log.i("tag", "onCreateView in TaskFragment");   
    fview = inflater.inflate(R.layout.progressfragment, container, false);
    bar = (ProgressBar) fview.findViewById(R.id.sendingSmsProgress);
    return fview;
}


}

感谢。

0 个答案:

没有答案