I\'ve an addActivity
hosting 5 fragments, on each fragments I\'ve some fields to fill.
I want to get the value of those fields from the addActivity
You are most likely experience this issue because you are not obtaining XML elements from the proper view or the view you are attempting to get data from gets destroyed before you attempt to access it.
However, I am not sure why exactly you are experiencing this issue but I do have a work-around solution do suggest:
Each time the user enters a field, you can store it in either SharedPreferences or a static
class that will serve as a storage unit.
I am not sure how all the data is being inputed by the user but you can use different listeners (such as OnClickListener
) to track when the user inputs a certain data field.
Then you can store the data using one of the two approaches i just mentioned above. Here is an example of using a static
storage class.
Somewhere in your application you make:
class Storage {
public static String dateFilled;
// ... rest of your data
}
Then once the user, to use the example you have in the question, stops entering information into the EditText
for dateFilled (they focus out of it), you store this information:
editText.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
// user is still typing so we do nothing
}else {
// user finished typing and moved on so we store the data
Storage.dateFilled = editText.getText().toString();
}
}
});
You could also use some sort of a Button
as the trigger to store information:
myBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Storage.dateFilled = editText.getText().toString();
}
}
Then you can use the stored data like:
public class getValueOfFields() {
Toast.makeText(getApplicationContext(), Storage.dateFilled, Toast.LENGTH_LONG);
// ...
}
If you want to use SharedPreferences
instead, here is a good example.