0

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>
HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Aditya Nigam
  • 869
  • 2
  • 11
  • 21

3 Answers3

2

You have to call fragmentManager.beginTransaction(); for every new transaction otherwise it will throw an exception

 if(fl.isVisible())
     {
         //fl.onActivityCreated(savedInstanceState);
         fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
         fragmentTransaction.commit();
     }else{
         //fl.onActivityCreated(savedInstanceState);
         fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
         fragmentTransaction.commit();
     }

and use setArgument to pass bundle without interrupting the fragment life cycle

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
0
// 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();
    }
    }
});
}
}
  • The code sure worked in switching from the fr (Registration Fragment) to fl (Login Fragment), but its not working the other way around. I tried changing the if -else conditions but now the app crashes on pressing the button second time. – Aditya Nigam Jul 01 '17 at 12:57
  • When you are going to press again the button , it checks the if(f1.isVisible()) condition but you replace the Login fragment to Registration so reference of login now not available . So take a variable checkflag for check this condition which have 0 value for Login Fragment and 1 for Registration and do like this if(checkFlag==0) { fragmentClass = FragmentRegistration.class; } else { fragmentClass = FragmentLogin.class; } – Sachin Chauhan Jul 03 '17 at 05:03
0
if(fl.isVisible())
     { 
         //put your argument like this
         Bundle args = new Bundle();
         args.putString(ARG_PARAM1, param1);
         fr.setArguments(args)

         fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
         fragmentTransaction.commit();
     }else{
         //put your argument like this
         Bundle args = new Bundle();
         args.putString(ARG_PARAM1, param1);
         fr1.setArguments(args)
         fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
         fragmentTransaction.commit();
     }
Mukesh Y.
  • 869
  • 1
  • 16
  • 36