当片段B 关闭时,我从片段A 打开片段B startActivityForResult 我从<获取字符串值strong> EditText 到片段A ,一切正常,但现在我从片段A 打开相同的片段B ,我想要它有那个早先的返回值,但它消失了
这里我存储了来自 editText 的 purposeText 值但未保存到json但我希望它在退出片段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;};
}
在片段A中我收到文字并将其设置为 textview 并且它可以正常工作,然后将其设置回 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;
}
}
现在在片段B 我试图将该文本设置回 onCreateView 方法中的 EditText ,但它获取默认值,我该怎么做才能解决这个问题?
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;
}
}
答案 0 :(得分:0)
在Fragment B
中,您要在Cloud
中创建一个新的onCreate()
对象,并尝试从onCreateView()
中获取该对象的值,该值始终为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;
}
对于您的任务,请使用SharedPreferences
或在实例化Fragment B
时传递保存的值。