Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6943585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:10:32+00:00 2026-05-27T13:10:32+00:00

I am adding and removing fragment dynamically in android while removing fragment .I am

  • 0

I am adding and removing fragment dynamically in android while removing fragment .I am getting this exception can anybody tell how to remove fragment?

12-09 15:00:16.363: ERROR/AndroidRuntime(673): FATAL EXCEPTION: main
12-09 15:00:16.363: ERROR/AndroidRuntime(673): java.lang.IllegalStateException: commit already called
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.app.BackStackRecord.commitInternal(BackStackRecord.java:499)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.app.BackStackRecord.commit(BackStackRecord.java:491)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at com.gcm.fragment.GcmFragmentActivity.onClick(GcmFragmentActivity.java:50)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.view.View.performClick(View.java:3110)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.view.View$PerformClick.run(View.java:11928)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.os.Handler.handleCallback(Handler.java:587)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.os.Handler.dispatchMessage(Handler.java:92)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.os.Looper.loop(Looper.java:132)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at android.app.ActivityThread.main(ActivityThread.java:4025)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at java.lang.reflect.Method.invoke(Method.java:491)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-09 15:00:16.363: ERROR/AndroidRuntime(673):     at dalvik.system.NativeStart.main(Native Method)

My code is below

package com.gcm.fragment;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class GcmFragmentActivity extends Activity implements OnClickListener {
    TextView txtIhaveIdea=null;
    boolean subMenu=false;
    Fragment fragOne = new SubFragment(); 
    FragmentManager fm = getFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtIhaveIdea=(TextView)findViewById(R.id.txtIhaveAnIdea);
        txtIhaveIdea.setOnClickListener(this);

        ; 
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.getId()==R.id.txtIhaveAnIdea)
        {
            if(!subMenu)
            {

                /*Bundle arguments = new Bundle(); 
                arguments.putBoolean("shouldYouCreateAChildFragment", true); 
                fragOne.setArguments(arguments); */
                //ft.show(fragOne);
                fm.beginTransaction(); 
                ft.add(R.id.main_frag_container, fragOne);              
                ft.commit();
                subMenu=true;
            }
            else if(subMenu)
            {
                fm.beginTransaction(); 
                ft.remove(fragOne);
                ft.commit();
                /*fm.beginTransaction(); 
                ft.replace(R.id.main_frag_container, null);                 
                ft.commit();*/
                subMenu=false;
                //ft.hide(fragOne);

                //ft.remove(fragOne);
                /*FragmentManager fm = getFragmentManager(); 
                FragmentTransaction ft = fm.beginTransaction(); 

                fm.beginTransaction(); 
                Fragment fragOne = new SubFragment(); 
                Bundle arguments = new Bundle(); 
                arguments.putBoolean("shouldYouCreateAChildFragment", true); 
                fragOne.setArguments(arguments); 
                ft.remove(fragOne); 
                ft.commit();    */
            }

        }

    }
}

/*FragmentManager fm = getFragmentManager(); 
FragmentTransaction ft = fm.beginTransaction(); 

fm.beginTransaction(); 
Fragment fragOne = new MyFragment(); 
Bundle arguments = new Bundle(); 
arguments.putBoolean("shouldYouCreateAChildFragment", true); 
fragOne.setArguments(arguments); 
ft.add(R.id.main_frag_container, fragOne); 
ft.commit(); 
*/

can anybody tell how to fix the problem

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:10:32+00:00Added an answer on May 27, 2026 at 1:10 pm

    Make ft a local variable, and not a class level variable

    public class GcmFragmentActivity extends Activity implements OnClickListener {
        TextView txtIhaveIdea=null;
        boolean subMenu=false;
        Fragment fragOne = new SubFragment(); 
        FragmentManager fm = getFragmentManager(); 
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            txtIhaveIdea=(TextView)findViewById(R.id.txtIhaveAnIdea);
            txtIhaveIdea.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v) {
            if(v.getId()==R.id.txtIhaveAnIdea){
            if(!subMenu){
                FragmentTransaction ft = fm.beginTransaction(); 
                ft.add(R.id.main_frag_container, fragOne);              
                ft.commit();
                subMenu=true;
            }else if(subMenu){
                FragmentTransaction ft = fm.beginTransaction();
                ft.remove(fragOne);
                ft.commit();
                subMenu=false;
            }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do i dynamically removing/adding(cancel the removal) form area using region and graphics path
I want to be able to modify Object dynamically by adding / removing properties
Basically, the codes I have is from here : http://ranafaisal.wordpress.com/2009/02/17/dynamically-adding-removing-textboxes-in-aspnet-repeater/ However, the thing is
I'm trying to display dynamically changeable data manipulating with DOM elements (adding/removing them). I
Is adding / removing HttpModules and HttpHandlers at runtime from a website a good
Visual Studio randomly crashes when adding/removing references and projects. Any thoughts why? Will installing
Am I right in assuming that adding/removing elements to an std::map does not effect
I was wondering why we use the terms push and pop for adding/removing items
I'm adding and removing <li> elements with jQuery, that are shown horizontally with the
I'm having trouble with adding and then removing an AjaxSelfUpdatingTimerBehavior in Apache Wicket. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.