用活动中的片段替换片段

时间:2014-03-14 04:48:36

标签: android android-fragments

我有一个Activity,其Fragment方法中添加了onCreate(Frag A)。此Fragment包含我希望活动在默认情况下显示的UI元素。在按钮上点击片段A,我想用另一个显示进度条的片段(片段B)替换片段A.如果在显示Frag B时存在配置,则在调用activity的onCreate方法时不应添加Frag(A)。

我怎样才能实现这个目标?

就像在这个Facebook应用程序中完成的那样:

First Screen showing UI Second sreeen showing progress bar after button click in

我尝试了很多方法:

  1. 首先,我直接在活动的布局中添加了UI元素以及空的framelayout,因此在按钮上单击我隐藏UI元素并执行Fragment事务以放置包含我的进度条的片段在framelayout中,这工作正常,但如果有配置更改,则重新创建活动,然后我回到原点。
  2. 这就是我现在所拥有的。

    MainActivity

        public class MainActivity extends FragmentActivity  {
    Button button;
    private ProgressDialog mpDialog;
    boolean running;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        MainFragment fragment = new MainFragment();
            FragmentTransaction fragmentTransaction=    getSupportFragmentManager().beginTransaction();
            fragmentTransaction.add(R.id.wrapper, fragment).commit();
    
    }
    
    
    
    public void Click() {
        TaskFragment taskFragment = new TaskFragment();
        getSupportFragmentManager().beginTransaction().replace(R.id.wrapper, taskFragment).commit();
    
    }
    
    
     }
    

    MainFragment(持有UI元素)

        public class MainFragment extends Fragment {
    
    View fView;
    ProgressBar bar;
    TextView textView;
    Button button;
    MainActivity activity = new MainActivity();
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        fView = inflater.inflate(R.layout.mainfragment, container, false);
        textView = (TextView) fView.findViewById(R.id.textview);
        button = (Button) fView.findViewById(R.id.button);
        clicked();
        return fView;
    }
    
    
    private void clicked() {
        // TODO Auto-generated method stub
        button.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
    
                activity.Click();
            }
        });
    
    }
    
       }
    

    TaskFragment(显示进度条)

       public class TaskFragment extends Fragment{
    
    View fview;
    ProgressBar bar;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
    }
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
    
        fview = inflater.inflate(R.layout.progressfragment, container, false);
        bar = (ProgressBar) fview.findViewById(R.id.sendingSmsProgress);
        return fview;
    }
    
     }
    

    我尝试切换片段时遇到的错误

      03-14 05:41:44.457: E/AndroidRuntime(11049): FATAL EXCEPTION: main
      03-14 05:41:44.457: E/AndroidRuntime(11049): java.lang.IllegalStateException: Activity has been destroyed
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1365)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at com.example.test.MainActivity.Click(MainActivity.java:41)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at com.example.test.MainFragment$1.onClick(MainFragment.java:47)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.view.View.performClick(View.java:4102)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.view.View$PerformClick.run(View.java:17085)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.os.Handler.handleCallback(Handler.java:615)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.os.Handler.dispatchMessage(Handler.java:92)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.os.Looper.loop(Looper.java:155)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at android.app.ActivityThread.main(ActivityThread.java:5454)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at java.lang.reflect.Method.invokeNative(Native Method)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at java.lang.reflect.Method.invoke(Method.java:511)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
      03-14 05:41:44.457: E/AndroidRuntime(11049):  at dalvik.system.NativeStart.main(Native Method)
    

1 个答案:

答案 0 :(得分:1)

如果要从片段中替换片段,请使用

getActivity().getSupportFragmentManager().beginTransaction()
            .replace(R.id.frame, new DialogFragment()).commit();