Puzirki
Puzirki

Reputation: 452

Android: Replacing of fragment removes other fragment

I use android support library and i have 3 fragments on activity. And i have a problem: replacing of fragment removes other fragment

My Activity:

public class MainActivity extends FragmentActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Add 3 fragments:

        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction()
                    .add(R.id.container1, new A(), A.class.getCanonicalName())
                    .commit();
            getFragmentManager().beginTransaction()
                    .add(R.id.container2, new B(), B.class.getCanonicalName())
                    .commit();
            getFragmentManager().beginTransaction()
                    .add(R.id.container3, new C(), C.class.getCanonicalName())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.test) {

            Fragment fragment = getFragmentManager().
                    findFragmentByTag(A.class.getCanonicalName());
            if (fragment == null)
                fragment = new A();

            // Replace fragment A again
                getFragmentManager()
                        .beginTransaction()
                        .replace(R.id.container1, fragment, fragment.getClass().getCanonicalName())
                        .commit();
            // After this fragment B disappears


            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

I use findFragmentByTag to avoid memory leak. Can you help me?

Upvotes: 0

Views: 2589

Answers (3)

Vivek Elangovan
Vivek Elangovan

Reputation: 248

public class MainActivity extends FragmentActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Add 3 fragments:

        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction()
                    .add(R.id.container1, new A(), A.class.getCanonicalName())
                    .commit();
            getFragmentManager().beginTransaction()
                    .add(R.id.container2, new B(), B.class.getCanonicalName())
                    .commit();
            getFragmentManager().beginTransaction()
                    .add(R.id.container3, new C(), C.class.getCanonicalName())
                    .commit();
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();
        if (id == R.id.test) {

            Fragment fragment = getFragmentManager().
                    findFragmentByTag(A.class.getCanonicalName());
            if (fragment == null)
                fragment = new A();

            // Replace fragment A again

            // After this fragment B disappears
FragmentManager fm = getFragmentManager();
             FragmentTransaction fragmentTransaction = fm.beginTransaction();
             fragmentTransaction.replace(R.id.container1, fragment);
fragmentTransaction.addToBackStack(null);
             fragmentTransaction.commit();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }

}

Try this

Upvotes: 0

Jogendra Gouda
Jogendra Gouda

Reputation: 405

FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack so the user can navigate back
transaction.replace(R.id.fragment_container, newFragment);  
transaction.addToBackStack(null);

// Commit the transaction
transaction.commit(); 

Try this code to replace any fragment

Upvotes: 1

Pankaj Arora
Pankaj Arora

Reputation: 10274

try this way:

make a common mathod to add fragments in your container,just put this code to your method and pass your fragment in that mehod,like:

public void _LoadFragment(Fragment _frag){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.container, _frag);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

Upvotes: 0

Related Questions