I can not find a way to successfully replace a fragment with another click on the button

advertisements

I went through some questions and made the changes as I thought were necessary, but the application still crashes every time I press the button to replace fragments. The fragments have the usual code and are just for simple layouts. On the launch, the activity will display the fragment I first add.

Here's my MainActivity:

public class MainActivity extends AppCompatActivity {

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

        final FragmentLogin fl = new FragmentLogin();
        final FragmentRegistration fr = new FragmentRegistration();
        final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        final android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragcon, fr);
        fragmentTransaction.commit();

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fl.isVisible())
                {
                    fl.onActivityCreated(savedInstanceState);
                    fragmentTransaction.replace(R.id.fragcon, fr);

                    fragmentTransaction.commit();
                }
                else  {
                    fl.onActivityCreated(savedInstanceState);
                    fragmentTransaction.replace(R.id.fragcon, fl);
                    fragmentTransaction.commit();
                }
            }
        });
    }
}

And here's my activitymain.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.aditya.registrationnlogin.MainActivity">

<RelativeLayout
    android:id="@+id/fragcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.8"
    android:layout_gravity="center_horizontal">

</RelativeLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="SWITCH" />

</LinearLayout>


// use this it will work sure dude !!
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final FragmentLogin fl = new FragmentLogin();
final FragmentRegistration fr = new FragmentRegistration();
final android.support.v4.app.FragmentManager fragmentManager =
getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragcon, fr);
fragmentTransaction.commit();

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Fragment fragment = null;
    Class fragmentClass = null;
        if(fl.isVisible())
        {
            fragmentClass = FragmentRegistration.class;
        }
        else  {
            fragmentClass = FragmentLogin.class;
        }

   try {
        fragment = (Fragment) fragmentClass.newInstance();
     } catch (Exception e) {
        e.printStackTrace();
     }

  // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    String backStateName = fragment.getClass().getName();
    boolean fragmentPopped = fragmentManager.popBackStackImmediate
  (backStateName, 0);

  if (!fragmentPopped && fragmentManager.findFragmentByTag(backStateName) ==
null){ //fragment not in back stack, create it.
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.flContent, fragment, backStateName);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
    }
});
}
}