美好的一天,我有一个导航抽屉活动。里面是碎片。我尝试通过更改方向来使用onSavedInstanceState。是的它正在发挥作用。但是当我从一个片段转到另一个片段时。它没有被召唤。请帮忙。我需要做的具体事情是在返回此片段时保存收件人列表。非常感谢你提前。这是我的代码:
@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);
}
答案 0 :(得分:0)
onSavedInstanceState
被调用,而不是在它正常结束时被调用。
您应该使用onPause
,onStop
或onDetach
中的一个。
由于系统不知道您计划实例化片段的新实例,因此您需要将其保存在其他位置,例如在SharedPreferences中:
得到:
saved_recipients = new ArrayList(sharedPreferences.getStringSet("recipients"));
集:
@Override
public void onPause(){
sharedPreferences.edit().putStringSet("recipients",new HashSet(recipients)).apply();
}
如果您不想永久保存此数据,但只有活动存在,请将其存储在活动本身中。对于片段 - 活动 - 沟通,请参阅Communicating between a fragment and an activity - best practices.