我有3个片段和3个按钮,当我点击一个按钮时,它会更改片段,但是当我点击片段列表上的某个项目时,它会打开一个新片段。
public class MainActivity extends FragmentActivity implements ContactFragment.OnContactClickedListener{
private CalendarFragment calendarFragment;
private CommunicationFragment communicationFragment;
private ContactFragment contactFragment;
private SingleContactFragment singleContactFragment;
//
private FragmentManager manager;
//
private Button btn_calendar, btn_contact, btn_communication;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bind();
initFragments();
manager = getSupportFragmentManager();
manager.beginTransaction()
.add(R.id.fl_main_container, calendarFragment).commit();
}
private void bind() {
btn_calendar = (Button) findViewById(R.id.btn_main_calendar);
btn_communication = (Button) findViewById(R.id.btn_main_communication);
btn_contact = (Button) findViewById(R.id.btn_main_contact);
btn_calendar.setOnClickListener(listener);
btn_communication.setOnClickListener(listener);
btn_contact.setOnClickListener(listener);
}
private void initFragments() {
calendarFragment = new CalendarFragment();
communicationFragment = new CommunicationFragment();
contactFragment = new ContactFragment();
singleContactFragment = new SingleContactFragment();
}
private void changeFragment(Fragment f) {
manager.beginTransaction().replace(R.id.fl_main_container, f).commit();
manager.executePendingTransactions();
}
private void displayContact(ContactObject contact){
manager.beginTransaction().replace(R.id.fl_main_container, singleContactFragment).addToBackStack(null).commit();
manager.executePendingTransactions();
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_main_calendar:
changeFragment(calendarFragment);
break;
case R.id.btn_main_communication:
changeFragment(communicationFragment);
break;
case R.id.btn_main_contact:
changeFragment(contactFragment);
break;
}
}
};
@Override
public void onClick(ContactObject contact) {
// TODO Auto-generated method stub
displayContact(contact);
}
}
当我点击联系人时,我选择一个联系人,然后我再次点击联系人,然后我开始点击我的后退按钮,一个" IllegalStateException;碎片已添加:ContactFragment"被扔了。而且我不明白为什么。
这里是logcat:
07-02 13:32:31.046: E/AndroidRuntime(11412): FATAL EXCEPTION: main
07-02 13:32:31.046: E/AndroidRuntime(11412): java.lang.IllegalStateException: Fragment already added: ContactFragment{420b6a40 #0 id=0x7f060000}
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1192)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:646)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:482)
07-02 13:32:31.046: E/AndroidRuntime(11412): at com.big5media.ercoiffure.MainActivity.changeFragment(MainActivity.java:57)
07-02 13:32:31.046: E/AndroidRuntime(11412): at com.big5media.ercoiffure.MainActivity.access$1(MainActivity.java:55)
07-02 13:32:31.046: E/AndroidRuntime(11412): at com.big5media.ercoiffure.MainActivity$1.onClick(MainActivity.java:79)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.view.View.performClick(View.java:4240)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.view.View$PerformClick.run(View.java:17721)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.os.Handler.handleCallback(Handler.java:730)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.os.Handler.dispatchMessage(Handler.java:92)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.os.Looper.loop(Looper.java:137)
07-02 13:32:31.046: E/AndroidRuntime(11412): at android.app.ActivityThread.main(ActivityThread.java:5103)
07-02 13:32:31.046: E/AndroidRuntime(11412): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 13:32:31.046: E/AndroidRuntime(11412): at java.lang.reflect.Method.invoke(Method.java:525)
07-02 13:32:31.046: E/AndroidRuntime(11412): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-02 13:32:31.046: E/AndroidRuntime(11412): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-02 13:32:31.046: E/AndroidRuntime(11412): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
在你的changeFragment方法上尝试使用addToBackStack(null)附加,如果你不需要导航回来......否则你最好删除片段并重新添加它。
manager.beginTransaction().replace(R.id.fl_main_container, f).addToBackStack(null).commit();