(I'm going to be VERY specific for the avoidance of doubt and because I really need to fix this. Sorry for the length of the question)
My app starts by displaying the first fragment of a navigation Drawer (Home). Inside this fragment there is a button called " Activity " which redirects me to an activity so I stay out of the nav drawer.
The activity has two Edit Texts ( First Name and Last Name ) and a button called " Send data " to send the data back to the first fragment of the navigation drawer ( Home ).
To achieve the transfer of data from the activity to the fragment I am trying two different ways (which are not giving me the desired results) which I am going to explain in detail:
The first method I use is the use of Bundle which for some reason doesn't work. It does not receive the data.
I attach the method that I put on the "Send Data" button that takes the data from the activity and then closes it to return to the "Home" fragment:
public void enviarDatos(){
Bundle bundle = new Bundle();
bundle.putString("a","tomas");
fragment_inicio f = new fragment_inicio();
f.setArguments(bundle);
finish();
}
Once the button is clicked, it returns to the "Home" fragment where the data should be captured (a situation that does not occur). I attach the code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inicio, container, false);
Bundle bundle = getArguments();
if(bundle != null){
String nombre = bundle.getString("a");
Toast.makeText(getActivity(), nombre, Toast.LENGTH_SHORT).show();
}
return view;
}
Since this didn't work for me, I decided to try SharedPreferences . With this I only get a problem when I press the " Send data " button, because pressing it does not launch the action I want (Send a Toast) but the record is saved in the preferences. This means that if I close the application and reopen it, the app detects that data has already been saved in the preferences and ends up showing the Toast.
I would like the Toast to be displayed in the fragment when I hit the button from the activity and not having to "refresh" the fragment.
I attach the code of the "Send Data" button of the activity
public void enviarDatos(){
SharedPreferences prefs =
getSharedPreferences("Preferencias",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("a","Tom");
editor.commit();
finish();
}
I attach the code of the fragment that receives the preference
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inicio, container, false);
SharedPreferences prefs =
getActivity().getSharedPreferences("Preferencias",0);
String nombre = prefs.getString("a","error");
if(nombre != "error"){
Toast.makeText(getActivity(), nombre, Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(getActivity(), "No llegaron los datos",
Toast.LENGTH_SHORT).show();
}
return view;}
With intent data is sent between activities. From an Activity to a fragment (which belongs to the same Activity) bundle is used. I leave you an example.
In Activity2, which is where you capture the editText and assuming that the EditText are called first and last name, you send the data with
intent putExtra
:In the ActivityNavDrawer, inside onCreate, you receive the data with
Bundle getIntent
, and send it to the start_fragment withbundle putString
:You receive it in the fragment, in onCreateView: