[英]Going from one fragment to another fragment savedInstanceState not working
Good day, I have a navigation drawer activity. 美好的一天,我有一个导航抽屉活动。 Inside those are fragments.
这些里面是碎片。 I tried using the onSavedInstanceState by changing the orientation.
我尝试通过更改方向来使用onSavedInstanceState。 And yes it is working.
是的,它正在工作。 But when i go from one fragment to another.
但是当我从一个碎片变成另一个碎片时。 It is not being called.
它没有被调用。 Please help.
请帮忙。 The specific thing that i need to do is to save the recipients list when going back to this fragment.
我需要做的特定事情是回到此片段时保存收件人列表。 Thank you so much in advance.
提前非常感谢您。 Here's my code:
这是我的代码:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_recipients,container,false);
if(savedInstanceState!=null){
System.out.println("savedInstanceState Called!");
saved_recipients = savedInstanceState.getStringArrayList("recipients");
System.out.println(saved_recipients);
}
else{
System.out.println("savedInstanceState not called!");
button = (Button) view.findViewById(R.id.button);
recipients = new ArrayList<String>();
recipients_backend = new ArrayList<String>();
container_recipients = (ListView) view.findViewById(R.id.listView);
arrayAdpt= new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, recipients);
container_recipients.setAdapter(arrayAdpt);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
String cNumber = null;
Uri contactData = data.getData();
Cursor c = getActivity().managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getActivity().getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
recipients.add(name + " " + cNumber);
System.out.println("New recipient added! Current list of recipients: " + recipients);
if (!(recipients.isEmpty())){
//button.setVisibility(View.INVISIBLE);
}
else
// button.setVisibility(View.VISIBLE);
recipients_backend.add(cNumber);
arrayAdpt.notifyDataSetChanged();
System.out.println(recipients_backend);
}
}
break;
}
}
@Override
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putStringArrayList("recipients",recipients);
}
onSavedInstanceState
is called when your fragments lifecycle is interrupted, not when it finishes normally. 当您的片段生命周期中断时(而不是在正常结束时),将调用
onSavedInstanceState
。
You should use one of onPause
, onStop
or onDetach
instead. 您应该改用
onPause
, onStop
或onDetach
。
As the system doesn't know you plan to instantiate a new instance of your fragment, you need to save somewhere else, eg in SharedPreferences: 由于系统不知道您打算实例化片段的新实例,因此需要保存其他位置,例如在SharedPreferences中:
Get: 得到:
saved_recipients = new ArrayList(sharedPreferences.getStringSet("recipients"));
Set: 组:
@Override
public void onPause(){
sharedPreferences.edit().putStringSet("recipients",new HashSet(recipients)).apply();
}
If you do not want to save this data permanently, but only as long as the activity lives, store it in the activity itself. 如果您不想永久保存此数据,而仅在活动存在时保存,则将其存储在活动本身中。 For Fragment-Activity-communication see Communicating between a fragment and an activity - best practices.
有关片段活动通信,请参见在片段和活动之间进行通信-最佳实践。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.