Reputation: 505
I am adding a fragment into a RelativeLayout
which is a small part of other fragment(containing viewpager) which is adding well, but when i am trying to vanish the fragment calling finish()
its not working.
xml code:
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Share you thoughts here..."
android:paddingLeft="10dp"
android:background="@drawable/edittext_status_background"
android:padding="20dp"
android:elevation="5dp"
android:id="@+id/editTextWritePostId"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:fitsSystemWindows="true"
android:background="#f9f9f9"
android:id="@+id/relativeLayoutIdNewsFeedForPostBar"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post"
android:textColor="#ffffff"
android:background="#1470a6"
android:id="@+id/postButtonId"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkInButtonId"
android:layout_toLeftOf="@+id/smileyButtonId"
android:layout_centerInParent="true"
android:src="@drawable/ic_checkin"
android:background="@android:color/transparent"
android:layout_marginRight="3dp"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/smiley"
android:layout_toLeftOf="@+id/imageUploadButtonId"
android:layout_centerInParent="true"
android:layout_marginRight="10dp"
android:id="@+id/smileyButtonId"
/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/camera"
android:layout_toLeftOf="@+id/videoUploadButtonId"
android:layout_centerInParent="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="10dp"
android:id="@+id/imageUploadButtonId"
/>
</RelativeLayout>
**//adding new fragment to below layout**
**<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayoutIdForSmiley"
>
</RelativeLayout>**
</LinearLayout>
Fragment code to remove new fragment:
smileyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SmileyShowFragment smileyFragment=new SmileyShowFragment();
FragmentManager fragmentManager=getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
if(!(smileyFragment.isVisible())) {
fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
}else {
relativeLayoutIdForSmiley.setVisibility(View.GONE);
}
}
});
I am trying to vanish the RelativeLayout with visibility gone and also vanish fragment with getActivity().finish()
but none of these working.can you help me please. I have searched internet a lot which are same as i am doing.
Upvotes: 0
Views: 54
Reputation: 2034
I guess I have figured out your issue. Here is what you are doing the mistake. In your smileButton onclick you are creating a new instance of SmileyShowFragment(SmileyShowFragment smileyFragment=new SmileyShowFragment();
) and checking weather the fragment is visible using if(!smileyFragment.isVisible())
. No matter how many time you trigger your click, this condition will be true always, as the fragment instance is created every time you the click the button. The solution is, create the instance of the fragment outside of the button click. Try some thing like this.
final SmileyShowFragment smileyFragment = new SmileyShowFragment();
smileyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = fragmentManager.findFragmentById(R.id.relativeLayoutIdForSmiley);
if(!(fragment instanceof SmileyShowFragment)) {
fragmentTransaction.replace(R.id.relativeLayoutIdForSmiley, smileyFragment, "Smiley").commit();
}else {
fragmentTransaction.remove(smileyFragment).commit();
}
}
});
Upvotes: 1
Reputation: 364
Have you tryed to use detach
or remove
on your fragment ?
Go check : https://developer.android.com/reference/android/app/FragmentTransaction.html
Upvotes: 0