Figvam
Figvam

Reputation: 49

Problems with Transaction of one fragment to another

I have working code , when user goes from 1 fragmeent to another, but problem is, that when New Fragment is opened, old fragment is also dispalyed under.

So user is seeing new fragment, but when he scroll down, he will also old fragment. Is there an option, to totally hide previous fragment?

if(userDTO.getMobile().isEmpty()) {
            new AlertDialog.Builder(getActivity())
                    .setView(getLayoutInflater().inflate(R.layout.test2, null))
                    .setPositiveButton(R.string.okaypopup2,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {

                                    AppCompatActivity activity = (AppCompatActivity) view.getContext();
                                    ProfileSetting myFragment = new ProfileSetting();
                                    activity.getSupportFragmentManager().beginTransaction()
                                            .replace(R.id.zopa, myFragment)
                                            //.addToBackStack(null)
                                            .commit();
                                }
                            })
                    .show();
        }

Maybe I'm doing something wrong.. i Expect to open new fragment, when popup message appears.

Thank you

Upvotes: 2

Views: 73

Answers (3)

Vadde Sreenivasulu
Vadde Sreenivasulu

Reputation: 1

Try this,

Fragment fragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAG_TAG);
if(fragment != null)
    getSupportFragmentManager().beginTransaction().remove(fragment).commit();

Upvotes: 0

Amine
Amine

Reputation: 2434

Change this statement

activity.getSupportFragmentManager().beginTransaction()
                                        .replace(R.id.zopa, myFragment)
                                        //.addToBackStack(null)
                                        .commit();

To this

activity.getSupportFragmentManager().beginTransaction()
                                        .replace(((ViewGroup)(getView().getParent())).getId(), myFragment)
                                        //.addToBackStack(null)
                                        .commit();

I think you are passing the fragment id not the Activity container id in replace

Upvotes: 1

MartijndeM
MartijndeM

Reputation: 176

Add a tag to your old Fragment, get a reference and remove it at any time that is convenient to you. Something along the lines of:

Fragment fragment = getSupportFragmentManager().findFragmentByTag(YOUR_FRAG_TAG);
if(fragment != null)
    getSupportFragmentManager().beginTransaction().remove(fragment).commit();

Upvotes: 0

Related Questions