-1

I am frustrated.
i want to get value from child fragment to parent fragment i had tries many method.
interface, viewModel, sharedpreferences but no one work.

I had follow this method

but it doesn't work for me.
here my code parent fragment:

public class Chart_Fragment extends Fragment
implements Product_Fragment.pf_interface {
String dt;
TextView tv;


public Chart_Fragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.chart_fragment, container, false);

    // create product_fragment as childfragment
    FragmentManager fm = getChildFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    Product_Fragment product_fragment = new Product_Fragment();
    ft.replace(R.id.fl_two, new Product_Fragment());
    ft.commit();

    return v;
}

public void onViewCreated(View v, Bundle savedInstanceState){
    super.onViewCreated(v,savedInstanceState);
    tv = (TextView)v.findViewById(R.id.rcv);
}

@Override
public void data(String d) {
    FragmentManager fragmentManager = getChildFragmentManager();
    Product_Fragment product_fragment = 
    (Product_Fragment)fragmentManager.findFragmentByTag("data");

    if(product_fragment != null){
        String dt = d;
        tv.setText(dt);
        }
   }
}

and my childfragment is :

public class Product_Fragment extends Fragment {
private pf_interface pf;

public Product_Fragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.product_fragment, container, false);

    Button b = (Button)v.findViewById(R.id.btn_sender);
    b.setOnClickListener(new btnClick());

    return v;
}

public void onAttachToParentFragment(Fragment fragment){
    try {
        pf = (pf_interface)fragment;
    }catch (ClassCastException e){
        throw new ClassCastException(
                fragment.toString()+ "must implement pf_interface"
        );
    }
}

public void onCreate(Bundle savedInstanceState){
    Log.i(TAG, "onCreate");
    super.onCreate(savedInstanceState);

    onAttachToParentFragment(getParentFragment());
}


private class btnClick implements View.OnClickListener{

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_sender:
                if(pf != null){
                    pf.data("button click interface");
                }break;
        }

    }
}

public interface pf_interface{
    void data(String d);
}

}

textview on parent fragment didn't show the string d ("button click interface")from child fragment

1 Answer 1

0

Why you're checking for child fragment in callback on your parent fragment ?

@Override
public void data(String d) {
    FragmentManager fragmentManager = getChildFragmentManager();
    Product_Fragment product_fragment = 
    (Product_Fragment)fragmentManager.findFragmentByTag("data");

    if(product_fragment != null){ // Why to check for child fragment nullability?
        String dt = d;
        tv.setText(dt);
    }
 }

You can directly use like this:

@Override
public void data(String d) {
    tv.setText(d);
}

Note: on any certain scenario if any of object is null, your callback won't be executed. You've already check for null reference.

Sign up to request clarification or add additional context in comments.

1 Comment

I had tried it but still not working. There is an error " java.lang.NullPointerException" on pf.data("button click interface");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.