0

Im having some troubles with retrieving a bundle value in my onClickListener method or any other method in general. I have comments besides the code for better clarification. So what happens is i get a value from activityA put it in a bundle and pass the values to my fragment. I get the value from the bundle and set in my setter method the value i get from the bundle is not null if i toast the value it shows the correct one on the screen. Below im posting my whole onCreateMethod, im having this issue for the past couple of days so any help is greatly appreciated. What i think happens is that when i try to get the value from the bundle in my onClickListener method it resets itself to 0 for some reason.

Thanks for your help in advance

My onCreateView method in fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_todo_list, container, false);
    FloatingActionButton floatingActionButton = view.findViewById(R.id.fab);
    recyclerView = (RecyclerView) view.findViewById(R.id.list);

    bundle = this.getArguments();
    if(bundle != null) {
        fk_id = bundle.getLong("fk");
        setId_2(fk_id); // i try to set the value
        Toast.makeText(getContext(), "iz fragmenta" + getId_2(), Toast.LENGTH_SHORT).show(); // this prints the correct value
        floatingActionButton.setOnClickListener(new View.OnClickListener() { //this button stops working if in the if statement
            @Override
            public void onClick(View v) {
                LayoutInflater li = LayoutInflater.from(getActivity());
                View popupView = li.inflate(R.layout.popup_layout, null);
                final EditText editText = popupView.findViewById(R.id.userInput);
                AlertDialog.Builder adb = new AlertDialog.Builder(getContext());

                adb.setView(popupView);
                adb.setCancelable(false)
                        .setPositiveButton("Dodaj", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                String naziv = editText.getText().toString();
                                Aktivnost_ ak = new Aktivnost_(naziv, "15-jun", fk_id, "kajetan", "todo");
                                dodajAktivnost(ak);
                                array.add(ak);
                                Toast.makeText(getContext(), "dodano" + getId_2(), Toast.LENGTH_LONG).show();

                            }
                        })
                        .setNegativeButton("Prekliči", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.cancel();
                                Toast.makeText(getContext(), "Preklical sem", Toast.LENGTH_LONG).show();
                            }
                        });
                AlertDialog alertDialog = adb.create();
                alertDialog.setCancelable(true);
                alertDialog.show();
            }
        });


        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayoutManager.VERTICAL));
        mmAdapter = new ToDoRecyclerViewAdapter(listAktivnosti(getId_2()), getContext(), mListener);

        mmAdapter.setOnItemClickListner(new ToDoRecyclerViewAdapter.onItemClickListner() {
            @Override
            public void onClick(long i) {
                Intent intent = new Intent(getActivity(), PodrobnostiActivity.class);
                intent.putExtra("key_id", i);
                startActivity(intent);
                Toast.makeText(getContext(), "" + i, Toast.LENGTH_SHORT).show();
            }
        });
        mmAdapter.setOnLongClick(new ToDoRecyclerViewAdapter.OnLongClickListener_() {
            @Override
            public void onLongClick(long i, String item) {
                if (item.equals("doing")) {
                    boolean update_1 = db.updateList(i, item);
                    if (update_1) {
                        //NAREDI SE LEPE ANIMACIJE
                        android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
                        ft.detach(TodoFragment.this).attach(TodoFragment.this).commit();

                        Toast.makeText(getContext(), "Dodano v bazo.!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(getContext(), "Prislo je do napake!", Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
        recyclerView.setAdapter(mmAdapter);
    }  
    return view;
}

EDITED

    private long fk_id;
public long getId_2() {
    return fk_id;
}
public void setId_2(long fk_id) {
    this.fk_id = fk_id;
}

EDIT_2

This part is in my activities onCreate

    Intent intent = getIntent();
    long id = intent.getLongExtra("intVariableName",0);

android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, TodoFragment.newInstance(id), "a");
    fragmentTransaction.commit();

And i made a newInstance method in my fragment.

    public static TodoFragment newInstance(long id) {
    TodoFragment fragment = new TodoFragment();
    Bundle b = new Bundle();
    b.putLong("fk",id);
    fragment.setArguments(b);
    return fragment;
}
newplayer
  • 25
  • 7

1 Answers1

0

Try the following:

  1. Since you already have access to the data within your variable fk_id in your Fragment, you can mark it as final, and pass it to your Toast:

Toast.makeText(getContext(), "dodano" + fk_id, Toast.LENGTH_LONG).show();

OR

  1. Try marking your getId_2 and setId_2 with synchronized as well as your onCreateView(), this way, the data shared between the two methods will be synchronized.
Red M
  • 2,609
  • 3
  • 30
  • 50
  • Tried it, but i cant access the FloationActionButton for some reason if its in the if(bundle != null) the button just becomes unclickable? – newplayer Jun 13 '18 at 14:39
  • Correct me if I'm wrong but I believe that your floatingactionbutton belongs to the activity and not the fragment. Try creating a dummy button in your fragment and check if you are able to pass in data from your bundle to that dummy button. The test can be something like: setting text for that button using the data within the bundle. – Red M Jun 13 '18 at 14:56
  • It belongs to the fragment, i have the button in R.layout.fragment_todo_list in a coordinatorlayout. What is really strange to me is that if i remove if(bundle != null) the button works again – newplayer Jun 13 '18 at 15:04
  • did you try passing data to a dummy button? – Red M Jun 13 '18 at 15:09
  • I did yes, same thing happend here. It doesnt do anything. if i check for null values in bundle – newplayer Jun 13 '18 at 15:15
  • Does the value of the bundle reset itself if its used inside a onclickfunction? because im really confused at whats going on here – newplayer Jun 13 '18 at 15:33
  • Please share how you are passing data in your activity. – Red M Jun 13 '18 at 15:37
  • You are saying that you are passing data from activity to fragment but the code that you shared does not show that. Refer to this link on how to send data from activity to fragment https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android – Red M Jun 13 '18 at 16:13
  • Im confused i asked this question here https://stackoverflow.com/questions/50824223/issue-with-fragment-transaction-and-passing-values-to-fragment and told me i should do it like that – newplayer Jun 13 '18 at 16:18