0

MainActivity.java

btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentOne fragment = new FragmentOne();
                fragmentTransaction.add(R.id.layoutFragmentContainer, fragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });

...

FragmentOne.java

public class FragmentOne extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,  ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list,container,false);
    }
}

When I click the button search , the fragment appears , but when I click it again the application crashes.Can someone help , I am new in android and I can't solve this problem.

.... log:

 Process: com.example.user1.volleyballmanager, PID: 26892
                                                                                     java.lang.IllegalStateException: commit already called
                                                                                         at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:625)
                                                                                         at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:617)
                                                                                         at com.example.user1.volleyballmanager.MainActivity$2.onClick(MainActivity.java:52)--- 
line 52 : fragmentTransaction.commit();
                                                                                         at android.view.View.performClick(View.java:5156)
                                                                                         at android.view.View$PerformClick.run(View.java:20755)
                                                                                         at android.os.Handler.handleCallback(Handler.java:739)
                                                                                         at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                         at android.os.Looper.loop(Looper.java:145)
                                                                                         at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Antoni Dobrenov
  • 162
  • 2
  • 9

2 Answers2

1

Put this method into your main activity

//FRAGMENT BACK STACK
public void getFragmentWithTag(Fragment fragment, String tag) {
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.replace(R.id.activityFrame, fragment, tag).commit();
}

You can call it from your fragment like this..

activity.getFragmentWithTag(Fragment, FragmentTag)
Laurence Pardz
  • 292
  • 3
  • 8
0

you cannot have multiple transaction commits. So you need to begin and close the transaction. So when you click one, it works. Then you click again, and it give you error which says one transaction<--> one commit.

Rusheel Jain
  • 843
  • 6
  • 20