• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

App Inventor Question: How to return to ParentFragment with RecyclerView.Adapter from a DialogFragment called tha

Degenhard

Lurker
I have a Fragment "MyParentFragment" (in a tabbed fragment in a NavigationDrawer Activity). In MyParentFragment sits a RecyclerView, fed from a SQLiteDB. When clicked on a RecyclerView item, a custom DialogFragment "myDialogFragment" is opened and a bundle containing a custom Parcelable object is passed. The DialogFragment contains a button, that does stuff (later).

So far, everything is working.

Now, when I click the button in the DialogFragment, after it did its stuff, I want to return to the DialogFragments parent fragment "MyParentFragment" that has the RecyclerView, if possible showing the RecyclerView item, that was last clicked. Everything I tried so far, crashed the app.

The DialogFragment does not have to return anything (currently a String), I can work around that, but it would be great if it could return a custom object of another class.

Min SDK version is 21, current target version is 32.

This is my first Android Project. I know some Java, but Android development, interfaces, backstacks and such are new to me. I hope you can help me.

This is MyParentFragment:

Java:
package com.example.utnmpg;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.utnmpg.Database.DataBaseHelper;
import com.example.utnmpg.Database.VPeopleModel;
import com.example.utnmpg.RecView.PeopleRecycleViewAdapter;

import java.util.ArrayList;

public class MyParentFragment extends Fragment
        implements View.OnClickListener, MyDialogFragment.DialogListener{

    private DataBaseHelper dataBaseHelper;
    private ArrayList<VPeopleModel> peopleList;
    private RecyclerView MyRecyclerView;
    private RecyclerView.Adapter mAdapter;

    public MyParentFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBaseHelper = new DataBaseHelper(getContext());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_myparrent,
                container, false);
        MyRecyclerView = view.findViewById(R.id.rv_peopleList);
        MyRecyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(view.getContext());
        MyRecyclerView.setLayoutManager(layoutManager);
        mAdapter = new PeopleRecycleViewAdapter(peopleList, view.getContext());
        MyRecyclerView.setAdapter(mAdapter);
        return view;
    }

    @Override
    public void onClick(View v) {
    }

    @Override
    public void onFinishNoDialog(String inputText) {
        Toast.makeText(getContext(), inputText, Toast.LENGTH_SHORT).show();
    }
}

This is the RecyclerView in MyParentFragment:

Java:
package com.example.utnmpg.RecView;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.example.utnmpg.Database.VPeopleModel;
import com.example.utnmpg.MyDialogFragment;
import com.example.utnmpg.R;

import java.util.ArrayList;


public class PeopleRecycleViewAdapter
        extends RecyclerView.Adapter<PeopleRecycleViewAdapter.MyViewHolder>
        implements View.OnClickListener, MyDialogFragment.DialogListener {

    private ArrayList<VPeopleModel> peopleList;
    private Context context;

    public PeopleRecycleViewAdapter(ArrayList<VPeopleModel> peopleListParam,
                                    Context contextP) {

        this.peopleList = peopleListParam;
        this.context = contextP;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.one_person,
                parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        holder.tvName.setText(peopleList.get(position).toStringName());
        holder.parentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyDialogFragment myDialogFragment = new MyDialogFragment();
                Bundle bundle = new Bundle();
                bundle.putParcelable("people", peopleList.get(holder.getBindingAdapterPosition()));
                myDialogFragment.setArguments(bundle);

                FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

                Fragment prev = fragmentManager.findFragmentByTag("people");
                if (prev != null) {
                    fragmentTransaction.remove(prev);
                }
                fragmentTransaction.addToBackStack(null);
                myDialogFragment.show(fragmentManager, "people");
            }
        });
    }

    @Override
    public int getItemCount() {
        return peopleList.size();
    }

    @Override
    public void onClick(View v) { }

    @Override
    public void onFinishNoDialog(String returnText) {
        Toast.makeText(context, returnText, Toast.LENGTH_SHORT).show();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView tvName;
        private ConstraintLayout parentLayout;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_ndia_name);
            //onePersonLayout is the id defined in one_person.xml
            parentLayout = itemView.findViewById(R.id.onePersonLayout);
        }
    }
}

And the DialogFragment:

Java:
package com.example.utnmpg;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import com.example.utnmpg.Database.DataBaseHelper;
import com.example.utnmpg.Database.VPeopleModel;

public class MyDialogFragment extends DialogFragment {
    private TextView tvName;
    private Button btnOK;
    private DataBaseHelper dataBaseHelper;

    public MyDialogFragment() {
    }

    public static MyDialogFragment newInstance(VPeopleModel peopleModel) {
        MyDialogFragment fragment = new MyDialogFragment();
        Bundle args = new Bundle();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBaseHelper = new DataBaseHelper(getContext());
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return super.onCreateDialog(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_my_dialog, container, false);
        Bundle bundle = getArguments();
        VPeopleModel peopleModel = bundle.getParcelable("people");
        tvName = view.findViewById(R.id.tv_ndia_name);
        btnOK = view.findViewById(R.id.btn_ndia_ok);

        tvName.setText(peopleModel.getName());

        btnOK.setOnClickListener(item -> {
            Toast.makeText(getContext(), "klicked", Toast.LENGTH_SHORT).show();
            String success = "yes";
            DialogListener dialogListener = (DialogListener) getParentFragment();
            dialogListener.onFinishNoDialog(success);
            dismiss();
        });
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    public interface DialogListener {
        void onFinishNoDialog(String inputText);
    }
}

Thank You
 
Back
Top Bottom