#closing a fragment

56 messages · Page 1 of 1 (latest)

vast trellis
#

hi, im trying to make a method to close a fragment inside the java file of the fragment
this is my code:

                closeFragment(view);
            }```
``` private void closeFragment(View view) {

        final FragmentTransaction ft = getParentFragmentManager().beginTransaction();
        ft.remove(new GalleryFragment());
        ft.commit();

    }```

it is not collopsing, it just doesnt do anything
fathom socketBOT
#

This post has been reserved for your question.

Hey @vast trellis! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fathom socketBOT
#

<@&765578700724371486>

Requested by ShadowRoar#3138
molten jungle
#

Pretty sure you need to track your reference the fragment you want to remove by giving it a tag when you passed it into the stack

vast trellis
molten jungle
#

One Google search could clear this out for you:

vast trellis
#

How can I find said tag or Id?

#

@molten jungle

molten jungle
#

Where are you adding the fragment

vast trellis
#

Inside an activity

#

Its a full screen fragment

molten jungle
#

Yes, you set the tag there

vast trellis
#

But what is the tag itself

#

Is it the name of the javafile? Xml?

#

if i try to do it crashes

#

        final FragmentTransaction ft = getParentFragmentManager().beginTransaction();
        ft.remove(getChildFragmentManager().findFragmentById(R.id.fragmentGallery));
        ft.commit();

    }```
molten jungle
#

how are you adding the fragment

#

please show that code

#

is it declared in XML already?

vast trellis
#
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragmentGallery"
    android:background="@android:color/holo_blue_dark"
    tools:context="com.example.projectyoavhofstein.GalleryFragment">

    <!-- TODO: Update blank fragment layout -->

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="@android:color/holo_green_light"
        android:gravity="top"
        android:id="@+id/shownCode"
        android:layout_marginBottom="275dp"
        android:padding="16dp"
        android:textColor="@android:color/white"
        android:textSize="22sp" />

    <ImageButton
        android:id="@+id/close"
        style="?android:attr/borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="345dp"


        android:onClick="closeFragment"
        android:src="@android:drawable/ic_menu_close_clear_cancel" />


</FrameLayout>```
#

import android.content.DialogInterface;
import android.os.Bundle;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;


import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import org.checkerframework.checker.nullness.qual.NonNull;


public class GalleryFragment extends FullScreenDialogFragment {
    View view;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {



        view = inflater.inflate(R.layout.fragment_gallery, container, false);

        ImageButton x = (ImageButton) view.findViewById(R.id.close);
        x.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                closeFragment(view);
            }
        });
        return view;
        }


    private void closeFragment(View view) {

        final FragmentTransaction ft = getParentFragmentManager().beginTransaction();
      Fragment help= getChildFragmentManager().findFragmentById(R.id.fragmentGallery);
      ft.remove(help);
        ft.commit();

    }
}```
vast trellis
# molten jungle is it declared in XML already?

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;

import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.appcompat.app.AppCompatDialogFragment;

/**
 * Fullscreen DialogFragment
 * Created by Allen on 2019/8/27.
 */
public class FullScreenDialogFragment extends AppCompatDialogFragment {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE,0);
    }

    @Override
    public void onStart() {
        super.onStart();
        //full screen
        Dialog dialog = getDialog();
        Window window;
        if (dialog != null && (window = dialog.getWindow()) != null) {
            int width = ViewGroup.LayoutParams.MATCH_PARENT;
            int height = ViewGroup.LayoutParams.MATCH_PARENT;
            window.setLayout(width, height);
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            window.addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            }
        }
    }
}```
molten jungle
#

This doesn't show of what I asked

#

where are you initially setting the fragment that you are removing?

vast trellis
#
        new GalleryFragment().show(getSupportFragmentManager(),
                "fullscreen dialog fragment");


    }```
#

this inaide the activity

molten jungle
#

is that second string argument not your tag?

vast trellis
#

it is the tag

molten jungle
#

so fragmentManager.getFragmentByTag("fullscreen dialog fragment")

vast trellis
#

should i put it inside the activty or inaise the fragment

molten jungle
#

put what

#

that is how you get the fragment so you can remove it

#

remove it wherever you were before

vast trellis
#

No but like should I put the code on the activity java file or the fragment java file

regal pawn
#

what are you trying to do

#

well yeah it doesn't remove an object because you're not removing it

#

instead you created a new object and removed it

vast trellis
molten jungle
vast trellis
molten jungle
#

the problem was you didn't have a way to reference the fragment you wanted to remove. now you do so what's the issue?

vast trellis
#

a lot of the online solutions don't work since I have a costum made fragment

regal pawn
#

you're referencing a new object

#

that you made

#

you're removing an object that you made in the same line you're removing it

vast trellis
#

So what am I suppose to replace it

regal pawn
#

guess

#

you should have at least basic java knowledge before doing anything...

vast trellis
#

@molten jungle thanks it worked

fathom socketBOT
# vast trellis <@361324748359335948> thanks it worked

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.