0

Good day, I have a navigation drawer activity. Inside those are fragments. I tried using the onSavedInstanceState by changing the orientation. 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);


}

1 Answers1

0

onSavedInstanceState is called when your fragments lifecycle is interrupted, not when it finishes normally.

You should use one of onPause, onStop or onDetach instead.


As the system doesn't know you plan to instantiate a new instance of your fragment, you need to save somewhere else, e.g. in 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.

Community
  • 1
  • 1
F43nd1r
  • 7,690
  • 3
  • 24
  • 62