2

I want to implement an activity that has a fragment! when I click on Fragment1 , Fragment2 is called , and when I click on Fragment2 , Fragment2 should be removed from the screen! I implement it by calling setOnclickListener of LinearLayout of Fragment2 in its onCreateView , and on my onclicklistener , I called

transaction.remove(myFragment);

transaction.commit();

but after that I faced to this error : commit already called how can I fix this error , here is my code: It is my fragment class

public class ArticleFragment extends Fragment {
final static String ARG_POSITION = "position";
int mCurrentPosition = -1;
private static LinearLayout l;
private android.support.v4.app.FragmentTransaction transaction;
private Fragment newFragment;

public void setArticleFragment(android.support.v4.app.FragmentTransaction transaction , Fragment newFragment) {
    this.transaction = transaction;
    this.newFragment = newFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        mCurrentPosition = savedInstanceState.getInt(ARG_POSITION);
    }
    View v = inflater.inflate(R.layout.article_view, container , false);
    l = (LinearLayout) v.findViewById(R.id.transparentArea);
    if(l == null)
    l.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            transaction.remove(newFragment);
            transaction.commit();
        }
    });
    return v;
}
}

and my mainAcitivity class

public class MainActivity extends FragmentActivity implements
    HeadlinesFragment.OnHeadlineSelectedListener{

HeadlinesFragment firstFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.news_articles);
    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }
        firstFragment = new HeadlinesFragment();
        firstFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
}

public void onArticleSelected(int position) {
    ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager()
            .findFragmentById(R.id.article_fragment);

    if (articleFrag != null) {
        articleFrag.updateArticleView(position);

    } else {
        final ArticleFragment newFragment = new ArticleFragment();
        Bundle args = new Bundle();
        args.putInt(ArticleFragment.ARG_POSITION, position);
        newFragment.setArguments(args);

        final FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        newFragment.setArticleFragment(transaction, newFragment);

        transaction.setCustomAnimations(R.anim.slidein, R.anim.slideout);
        transaction.add(R.id.fragment_container, newFragment);
        transaction.commit();

    }
}
Sharath
  • 691
  • 8
  • 23
Foad Saeidi Nik
  • 260
  • 1
  • 5
  • 20

3 Answers3

11

Instead of reusing the passed-in transaction, create a new FragmentTransaction instance that removes Fragment2.

Even easier is to add the first fragment transaction to fragment back stack (e.g. addToBackStack(null)) and then in Fragment2, just pop the back stack with FragmentManager popBackStack().

laalto
  • 150,114
  • 66
  • 286
  • 303
3

Begin new transaction

fragTran = fragMan.beginTransaction();
fragTran.replace(R.id.frag_cont_two, fThree);
fragTran.addToBackStack(null);
fragTran.commit();
Anand Savjani
  • 2,484
  • 3
  • 26
  • 39
0

I solved the issue after calling commit() and again replacing the fragment you should start from

fragmentTransaction = fragmentManager.beginTransaction();
Fazal
  • 3,374
  • 1
  • 15
  • 20