Reputation: 117
So I'm having an issue where when I click on my floating action button, the next fragment pops up but the other stays. What I want to happen is when I click the button, it disappears along with the fragment its on, then have the next fragment pop up.
Here is the fragment class with the button
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class ScoutFragment extends Fragment {
FloatingActionButton addDataScout;
public ScoutFragment() {
// Required empty public constructor
} //End of ScoutFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scout, container, false);
view.setBackgroundColor(Color.WHITE);
//Setups Floating Action Button
FloatingActionButton addDataScout = (FloatingActionButton) view.findViewById(R.id.fab);
addDataScout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AddScoutDataFragment fragment = new AddScoutDataFragment();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left);
fragmentTransaction.replace(R.id.layoutScout, fragment);
fragmentTransaction.commit();
} //End of onClick
}); //End of setOnClickListener
return view;
} //End of onCreateView
} //End of class
Here is the fragment class that I want to pop up
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class AddScoutDataFragment extends Fragment {
public AddScoutDataFragment() {
// Required empty public constructor
} //End of AddScoutDataFragment
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_add_scout_data, container, false);
} //End of onCreateView
} //End of class
Here is the xml file of the button fragment
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layoutScout"
tools:context="com.compscitutorials.basigarcia.ramfernoscout.ScoutFragment">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="61dp"
android:layout_height="57dp"
android:src="@mipmap/ic_action_add"
app:backgroundTint="#F28016"
app:elevation="5dp"
app:borderWidth="0dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="12dp"
android:layout_marginRight="10dp"
android:scaleType="fitCenter"
android:clickable="true">
</android.support.design.widget.FloatingActionButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Western University"
android:id="@+id/testText"
android:layout_centerInParent="true"
android:textSize="24dp"
android:textStyle="bold" />
</RelativeLayout>
</FrameLayout>
Here is the xml file for the fragment I want to pop up
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_add_scout_data"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.compscitutorials.basigarcia.ramfernoscout.AddScoutDataFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Accept Me"
android:id="@+id/testViewData"
android:layout_gravity="center"
android:textSize="24dp"
android:textStyle="bold" />
</FrameLayout>
Starting my fragments through MainAcitivty which extends AppCompatActivity and implements NavigationView
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_welcome) {
//Set the fragment initially
WelcomeFragment fragment = new WelcomeFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
// Handle the camera action
}
else if (id == R.id.nav_facebook) {
FacebookFragment fragment = new FacebookFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_members) {
//Set the fragment initially
MembersFragment fragment = new MembersFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_robot) {
}
else if (id == R.id.nav_scout) {
//Set the fragment initially
ScoutFragment fragment = new ScoutFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
}
else if (id == R.id.nav_match) {
//Set the fragment initially
MatchFragment fragment = new MatchFragment();
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment);
fragmentTransaction.commit();
} //End of if statement
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
} //End of onNavigationItemSelected
Upvotes: 0
Views: 171
Reputation: 129
I think it is because you pass the id of a view within the current fragment to the replace method. That node in the view hierarchy currently does not contain a fragment, so it will just be added. You will have to pass the id of the view which contains the ScoutFragment. (If you inflated the fragment using xml the id should be the one on the <fragment>
tag. If you inflated it from code pass the same id as you passed in your activity)
Upvotes: 1