-1

Im developing and App and I need to pass int data from a fragment to Another Activity. The problem its that it returns 0 or false.

First I start ActivityForResult putting a Bundle. Then I pass a boolean and it works fine but when I try to put the int into the bundle from the fragment and comeback to onActivityResult it retuns 0 or false.

Can someone help please? I have tried with putExtras too and still false

 public void newcampeon(View view) {
        Intent i = new Intent(getApplicationContext(), Campeones.class);
        b = new Bundle();
        Boolean vengodraft = true;
        b.putBoolean("vengodraft",vengodraft);
        i.putExtras(b);
        startActivityForResult(i,01);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
            int res = b.getInt("idcampeon");
           aban1.setImageResource(res);
            Toast.makeText(getApplicationContext(),String.valueOf(res),
                 Toast.LENGTH_SHORT).show();

    }

------ Fragment on Another Acivity

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_top_lane, container, false);
        b = getArguments();
        if(b!=null){
            Boolean vengodraft = b.getBoolean("vengodraft");
            if(vengodraft==true){
                final ImageView aatrox = v.findViewById(R.id.aatrox);
                aatrox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int id = aatrox.getId();
                        getActivity().setResult(Activity.RESULT_OK);
                        b.putInt("idcampeon",id);
                        getActivity().finish();

                    }
                });
            }
        }
        return v;

It should return the current id of the Img

Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

Finally I got what its wrong. Its very simple. I just put correctly the sintax here. Just make a new intent and pass the arguments .

 @Override
                    public void onClick(View v) {             
                        String nombrecampeon = String.valueOf(aatrox.getTag());
                        Intent i = new Intent();
                        i.putExtra("nombrecampeon",nombrecampeon);
                        getActivity().setResult(Activity.RESULT_OK,i);
                        getActivity().finish();
                    }
                });

And on ActivityResult

  String uri = "@drawable/"+data.getExtras().getString("nombrecampeon"); 
       int imageResource = getResources().getIdentifier(uri, null, getPackageName());
       Drawable res = getResources().getDrawable(imageResource);
       aban1.setImageDrawable(res);

Anyway, thanks all for the help :)