Alica Corwin Alica Corwin - 2 months ago 8
Android Question

Unable to start new fragment from a fragment

Hello there i want to start the fragment "Add_Create_2" from "Add_Create" here is my code both the fragments are merged and the previous fragment is not removed and so thats why the next fragment is shown on the top of the first fragment when ever i click next button that is present in Add_Create Layout

Add_Create Java Code
@Override
public void onClick(View view) {
FragmentTransaction transaction=getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.root,new Add_Create_2())
;
transaction.addToBackStack(null);

transaction.commit();

}

Add_Create XML Code
<ScrollView 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:fillViewport="true">

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/root"

android:layout_height="match_parent">

<TextView
android:text="General Information"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="15dp"
android:id="@+id/textView1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="20dp"
android:textStyle="bold"
android:layout_alignParentTop="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:hint="Enter Announcement Name"
android:backgroundTint="@color/lightblue"
android:textColorHint="#3F51B5"
android:textColor="#3F51B5"
android:id="@+id/announcement_name"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:hint="Enter Description"
android:backgroundTint="@color/lightblue"
android:textColorHint="#3F51B5"
android:textColor="#3F51B5"
android:id="@+id/description"
android:maxLines="4"
android:lines="2"
android:layout_below="@+id/announcement_name"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/lightblue"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:hint="Enter Price"
android:textColorHint="#3F51B5"
android:textColor="#3F51B5"
android:id="@+id/price"
android:layout_below="@+id/description"
android:layout_centerHorizontal="true" />
<Spinner
android:id="@+id/duration"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/items"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"

android:layout_below="@+id/price"
android:prompt="@string/duration" />

<TextView
android:text="Choose Image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="15dp"
android:id="@+id/image1"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="20dp"
android:layout_below="@+id/duration" />
<Button
android:text="Choose"
android:layout_width="wrap_content"
android:layout_below="@+id/duration"
android:layout_height="wrap_content"
android:layout_marginStart="21dp"
android:id="@+id/Image_Picker"
android:layout_gravity="right"
android:layout_marginTop="20px"
android:textColor="#FFFFFF"
android:layout_marginLeft="150dp"
android:background="@drawable/buttons"
android:layout_centerHorizontal="true"
android:layout_toRightOf="@+id/image1" />
<TextView
android:text="Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="15dp"
android:id="@+id/textView2"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_below="@+id/image1"
android:textColor="@color/colorPrimary"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:hint="Enter Location"
android:backgroundTint="@color/lightblue"
android:textColorHint="#3F51B5"
android:textColor="#3F51B5"
android:id="@+id/room_address"
android:maxLines="4"
android:lines="2"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true" />
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_below="@+id/room_address"
android:layout_height="wrap_content"
android:id="@+id/Submit_Room_Information"
android:textColor="#FFFFFF"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:background="@drawable/buttons" />

</RelativeLayout>




Add_Create_2 Java Code
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

return inflater.inflate(R.layout.create_add_2, container, false);
}

Add_Create_2 XML Code
<?xml version="1.0" encoding="utf-8"?>


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent">
<TextView
android:text="Availability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="15dp"
android:id="@+id/textView5"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="20dp"
android:textStyle="bold" />
<CheckBox
android:id="@+id/checkbox_alltheday"
android:layout_width="wrap_content"
android:layout_below="@+id/textView5"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:textColor="@color/colorPrimary"
android:layout_marginLeft="10dp"
android:text="All the Day" />
<TextView
android:text="Start Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="15dp"
android:id="@+id/textView7"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_below="@+id/checkbox_alltheday"
android:textColor="@color/colorPrimary"
android:textSize="20dp" />
<TextView
android:text="Services"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteX="151dp"
tools:layout_editor_absoluteY="15dp"
android:id="@+id/textView9"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:textColor="@color/colorPrimary"
android:textSize="20dp"
android:layout_below="@+id/textView8"

android:textStyle="bold" />

<CheckBox
android:id="@+id/service_bathroom"
android:layout_width="wrap_content"
android:layout_below="@+id/textView9"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:textColor="@color/colorPrimary"
android:layout_marginLeft="10dp"
android:text="Bathroom" />

android:text="" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/service_other"
android:layout_marginTop="17dp"

android:backgroundTint="@color/lightblue"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:hint="Other"
android:textColorHint="#3F51B5"
android:textColor="#3F51B5"
android:id="@+id/other"
android:layout_below="@+id/service_parking"
/>



here is the result

Answer

Transitions should be handled by Activity not by fragment. When you get the event to change the fragment then give this information to your activity, after that activity should change the fragment. i.e. Make a function in activity which performs the transitions and call that function from the fragment whenever required. You can all the method of activity either by creating an interface or typecasting getActivity() method in your activity class type to call the method

This should fix your code MainActivity xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="yourapp.com.fragments.MainActivity">

    <FrameLayout
        android:id="@+id/root"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
    FrameLayout root;
    AddCreate firstFragment;
    AddCreate2 secondFragment;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        firstFragment = new AddCreate();
        secondFragment = new AddCreate2();
        showFragment(0);

    }

    public void showFragment(int which) {
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        switch (which) {
            case 0:
                transaction.replace(R.id.root,firstFragment);
                break;
            case 1:
                transaction.replace(R.id.root,secondFragment);
                break;
        }
        transaction.addToBackStack(null);
        transaction.commit();
    }
}

AddCreate.java

public class AddCreate extends Fragment {


    public AddCreate() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View v =  inflater.inflate(R.layout.add__create, container, false);
        v.findViewById(R.id.Submit_Room_Information).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ((MainActivity)getActivity()).showFragment(1);
            }
        });
        return v;
    }

}

AddCreate.xml

<ScrollView 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">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="pantherstechnik.com.fragments.fragment.AddCreate">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="General Information"
            android:textColor="@color/colorPrimary"
            android:textSize="20dp"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="151dp"
            tools:layout_editor_absoluteY="15dp" />

        <EditText
            android:id="@+id/announcement_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView1"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:hint="Enter Announcement Name"
            android:textColor="#3F51B5"
            android:textColorHint="#3F51B5" />

        <EditText
            android:id="@+id/description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/announcement_name"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"

            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:hint="Enter Description"
            android:lines="2"
            android:maxLines="4"
            android:textColor="#3F51B5"
            android:textColorHint="#3F51B5" />

        <EditText
            android:id="@+id/price"
            android:layout_width="match_parent"

            android:layout_height="wrap_content"
            android:layout_below="@+id/description"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:hint="Enter Price"
            android:textColor="#3F51B5"
            android:textColorHint="#3F51B5" />

        <Spinner
            android:id="@+id/duration"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_below="@+id/price"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"

            android:layout_marginTop="10dp"
            />

        <TextView
            android:id="@+id/image1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/duration"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="Choose Image"
            android:textColor="@color/colorPrimary"
            android:textSize="20dp"
            tools:layout_editor_absoluteX="151dp"
            tools:layout_editor_absoluteY="15dp" />

        <Button
            android:id="@+id/Image_Picker"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/duration"
            android:layout_centerHorizontal="true"
            android:layout_gravity="right"
            android:layout_marginLeft="150dp"
            android:layout_marginStart="21dp"
            android:layout_marginTop="20px"
            android:layout_toRightOf="@+id/image1"

            android:text="Choose"
            android:textColor="#FFFFFF" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/image1"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="20dp"
            android:text="Location"
            android:textColor="@color/colorPrimary"
            android:textSize="20dp"
            android:textStyle="bold"
            tools:layout_editor_absoluteX="151dp"
            tools:layout_editor_absoluteY="15dp" />

        <EditText
            android:id="@+id/room_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/textView2"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="10dp"

            android:layout_marginRight="10dp"
            android:layout_marginTop="20dp"
            android:hint="Enter Location"
            android:lines="2"
            android:maxLines="4"
            android:textColor="#3F51B5"
            android:textColorHint="#3F51B5" />

        <Button
            android:id="@+id/Submit_Room_Information"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/room_address"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="20dp"
            android:layout_marginTop="10dp"
            android:text="Next"
            android:textColor="#FFFFFF" />

    </RelativeLayout>

</ScrollView>

AddCreate2 xml

<ScrollView 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"
    tools:context="pantherstechnik.com.fragments.fragment.AddCreate2">
    <LinearLayout
        android:orientation="vertical" android:layout_width="match_parent"
             android:layout_height="match_parent">
    <TextView
        android:text="Availability"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="151dp"
        tools:layout_editor_absoluteY="15dp"
        android:id="@+id/textView5"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/colorPrimary"
        android:textSize="20dp"
        android:textStyle="bold" />
    <CheckBox
        android:id="@+id/checkbox_alltheday"
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:textColor="@color/colorPrimary"
        android:layout_marginLeft="10dp"
        android:text="All the Day" />
    <TextView
        android:text="Start Time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="151dp"
        tools:layout_editor_absoluteY="15dp"
        android:id="@+id/textView7"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:layout_below="@+id/checkbox_alltheday"
        android:textColor="@color/colorPrimary"
        android:textSize="20dp" />
    <TextView
        android:text="Services"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteX="151dp"
        tools:layout_editor_absoluteY="15dp"
        android:id="@+id/textView9"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="20dp"
        android:textColor="@color/colorPrimary"
        android:textSize="20dp"
        android:layout_below="@+id/textView8"

        android:textStyle="bold" />

    <CheckBox
        android:id="@+id/service_bathroom"
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView9"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginRight="10dp"
        android:textColor="@color/colorPrimary"
        android:layout_marginLeft="10dp"
        android:text="Bathroom" />

    android:text="" />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/service_other"
        android:layout_marginTop="17dp"
        android:layout_marginRight="10dp"
        android:layout_marginLeft="10dp"
        android:hint="Other"
        android:textColorHint="#3F51B5"
        android:textColor="#3F51B5"
        android:id="@+id/other"
        android:layout_below="@+id/service_parking"
        />
    </LinearLayout>
</ScrollView>

AddCreate2.java

public class AddCreate2 extends Fragment {


    public AddCreate2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.add_create2, container, false);
        return v;
    }

}