Removing fragment after adding does not work
My problem is that I want to remove Fragment
from Activity. There are two steps:
- Add
Fragment
toFrameLayout
(R.id.frame_for_des_details) checkingCheckBox
. -
Delete
Fragment
by pushingTextView
public class CreateTripActivity extends FragmentActivity { //Vars for Destination Fragment private CheckBox checkBox; private DestinationDetailsFragment destinationDetailsFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Setting the ContentView setContentView(R.layout.activity_create_trip); //Set the Destination Checkbox listener checkBox = (CheckBox) this.findViewById(R.id.checkBox_destination_now);
Listener for CheckBox
:
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
//Adding the Fragment. THIS WORK
destinationDetailsFragment = new DestinationDetailsFragment();
FragmentManager fm = getFragmentManager();
fm.beginTransaction().add(R.id.frame_for_des_details, destinationDetailsFragment).commit();
}
});
Listener for TextView
:
findViewById(R.id.label_later).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Removing the Fragment. DOES NOT WORK
FragmentManager fm = getFragmentManager();
fm.beginTransaction().remove(destinationDetailsFragment).commit();
}
});
}
When I check CheckBox
, I can see the Fragment. But when I click TextView
to delete it, it remains. When I check over and over again CheckBox
, it seems like the new Fragment
overlays the old snippet, so I can write on top of the placeholders in EditText
.
Hope someone can understand me and can help me.
Thank.
source to share
Try the android.support.v4.app.FragmentManager file. Here is the code.
destinationDetailsFragment = new DestinationDetailsFragment();
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton,
boolean b) {
// Adding the Fragment. THIS WORK
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.add(R.id.frame_for_des_details,
destinationDetailsFragment).commit();
}
});
findViewById(R.id.label_later).setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction()
.remove(destinationDetailsFragment).commit();
}
});
source to share
Fixed
I am implementing the Fragment as android.support.v4.app.Fragment but this is not a problem.
Before adding the snippet, I unchecked the box. And bevor deleting the Fragment, I add the checkbox to the view again. But I also named checkbox.setChecked (false). This fired onCheckedChanged again!
My new code
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
if (isChecked) {
LinearLayout layout = (LinearLayout) findViewById(R.id.layout_destination);
//Remove and Add Content
layout.removeView(checkBox);
destinationDetailsFragment = new DestinationDetailsFragment();
FragmentManager fm = getSupportFragmentManager();
fm.beginTransaction().add(R.id.frame_for_des_details, destinationDetailsFragment).commit();
//Enable Later Button
findViewById(R.id.img_later).setVisibility(View.VISIBLE);
findViewById(R.id.label_later).setVisibility(View.VISIBLE);
}
}
Thank.
source to share