kosas kosas - 5 months ago 12
Android Question

value disapears when trying to get it back on second Fragment

I opening Fragment B from Fragment A with startActivityForResult when Fragment B closes I get String value from EditText to Fragment A and everything works fine but now I opening same Fragment B from Fragment A and I want it to have that earlier returned value but it disappears

here I'm storing purposeText value from editText but not saving to json yet I want it to be saved on exiting Fragment A

public class Cloud {


private static final String JSON_ID = "id";
private static final String JSON_PURPOSE = "purpose";

private UUID mId;
private String purposeText = "";



public Cloud() {

mId = UUID.randomUUID();
}



public Cloud(JSONObject json) throws JSONException {
mId = UUID.fromString(json.getString(JSON_ID));
if (json.has(JSON_PURPOSE)){
purposeText = json.getString(JSON_PURPOSE);
}

}

public JSONObject toJSON() throws JSONException{

JSONObject json = new JSONObject();
json.put(JSON_ID, mId.toString());
json.put(JSON_PURPOSE, purposeText);

return json;
}


@Override
//Using get and set methods to get it in private String
public String getPurposeText(){return purposeText;}
public void setPurposeText(String purpose){purposeText = purpose;}



public UUID getId() {return mId;};

}


In Fragment A I'm getting text and setting it to textview and it works, then setting it back to setter in Cloud class

public class AddFragment extends Fragment {

private static final String TAG = "Cload";
public static final String EXTRA_DREAM_ID = "com.example.tadas.dreamcload1.dream_id";
private static final int REQUEST_DATE = 0;
private static final int REQUEST_TEXT =1;

private Cloud dCloud;
private TextView purposeView;
private String comeBackPurpose;



@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

dCloud = new Cloud();
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

getActivity().setTitle("Add new dream");

}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup parent, Bundle savedInstanceState){

View v = inflater.inflate(R.layout.fragment_add,parent,false);

purposeView = (TextView)v.findViewById(R.id.dream_purpose);
purposeView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getActivity(),DreamPurposeActivity.class);
startActivityForResult(i,REQUEST_TEXT);
}
});

return v;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == REQUEST_TEXT){

comeBackPurpose = data.getStringExtra("comeBackPurpose");
// Log.e(TAG,"SHITFACE " + comeBackPurpose);
purposeView.setText(comeBackPurpose);
dCloud.setPurposeText(comeBackPurpose);
Log.e(TAG,"SHITFACE " + dCloud.getPurposeText());
}
}


public static AddFragment newInstance(UUID dreamId) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_DREAM_ID, dreamId);

AddFragment fragment = new AddFragment();
fragment.setArguments(args);

return fragment;
}
}


Now In Fragment B I'm trying to set that text back to EditText in onCreateView method but it gets default value, what should I do to fix that?

public class PurposeFragment extends Fragment {

public static final String EXTRA_D = "com.dropstudio.drea.dr_ids";
private static final String TAG = "Cload";

private EditText purposeEText;
private String purposeEditText;
private Cloud mCloud;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mCloud = new Cloud();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.dream_purpose_edit,container,false);
purposeEText = (EditText)v.findViewById(R.id.purpose_edit);


purposeEText.setText(mCloud.getPurposeText());

return v;
}


public void savePurpose(){

purposeEditText = purposeEText.getText().toString();
mCloud.setPurposeText(purposeEditText);
Intent returnIntent = new Intent();
returnIntent.putExtra("comeBackPurpose",purposeEditText);
getActivity().setResult(Activity.RESULT_OK,returnIntent);
getActivity().finish();


}

public static PurposeFragment newInstance(UUID dreaIds) {
Bundle args = new Bundle();
args.putSerializable(EXTRA_D, dreaIds);

PurposeFragment fragment = new PurposeFragment();
fragment.setArguments(args);

return fragment;
}
}

Answer

In Fragment B , you are creating a new Cloud object in onCreate() and trying to get the value from that object in onCreateView() which will always be null.

 @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mCloud = new Cloud(); //New instance will be created
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.dream_purpose_edit,container,false);
        purposeEText = (EditText)v.findViewById(R.id.purpose_edit);


        purposeEText.setText(mCloud.getPurposeText()); // 'mCloud' is a new object everytime and hence will not have your saved value

        return v;
    }

For your task, either use SharedPreferences or pass the saved value while instantiating Fragment B.

Comments