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;
}