values are set to null after onActivityResult

2019-08-01 13:20发布

I have a DialogFragment class with a custom xml layout. In onCreateView() I get reference to a button. You click it to choose file. When it's clicked two things happen: 1) It uses a layout inflater to add another xml layout into the current one 2) It initializes three variables to their values ListView, ListArray, SimpleAdapter, and these are member variables declared at the top of the class. 3) Then it calls getActivity().startActivityForResult()

Problem: The three variables described above are set to null when I come back into this activity to the dialogfragment.



Here is a detailed version of what's happening:

//before calling startActivityForResult()
I/NULLTEST SIMPLEADAPTER﹕ android.widget.SimpleAdapter@64cf1348
I/NULLTEST ATTACHMENTSLISTVIEW﹕ android.widget.ListView{64d1c2e0 VFED.VC. ......I. 0,0-0,0 #7f090014 app:id/attachmentsListView}
I/NULLTEST ATTACHMENTSLISTARRAY﹕ [{a_date_added=Added: Nov 1, a_name=My test file 0}, {a_date_added=Added: Nov 2, a_name=My test file 1}, {a_date_added=Added: Nov 3, a_name=My test file 2}]
// Inside onActivityResult()
I/NULLTEST SIMPLEADAPTER ONRESULT﹕ null
I/NULLTEST ATTACHMENTSLISTVIEW ONRESULT﹕ null
I/NULLTEST ATTACHMENTSLISTARRAY ONRESULT﹕ []

This whole code is being called inside a DialogFragment. As you can see above before calling startActivityForResult() the three variables SimpleAdapter, ListView, and ArrayList are set to some value. They are all member variables thus can be called anywhere in this class. Inside onCreateView() method of DialogFragment I get reference to a button part of the dialog and attach a setOnClickListener() to the button. When this button is clicked, the three variables described above are initialized to their values and I use Log.i to log the values. The button onClick() also calls the getActivity().startActivityForResult(); to allow me to choose a file thus opening a completely different Activity. I choose the file and the onActivityResult() method of the host Activity is called which for my case is MainActivity.java. From here the data is passed onto the DialogFragment as such:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

Now I have access to the data inside the DialogFragment but in the onActivityResult of the DialogFragment not the Activity, the three variables described above are set to null but they are only initialized on the button click but that would call startActivityForResult() and so on. I do not understand why they are set back to null. Am I missing something important here? Due to this I cannot call for example simpleAdapter.notifyDataSetChanged(). This exact way of updating my listview works if I do this in an Activity instead of a DialogFragment but I need to do this inside a DialogFragment. Also any other ways of updating listview after retrieving data from another activity are welcome. I just need a way to update that listview when I select more data.

FINAL UPDATE

I would like to apologize to for the height of stupidity that I was making. In my hosting Activity which is MainActivity.java, when this Activity would call onActivityResult() I would create a new instance of the dialog fragment as such: new MyDialogFragment().onActivityResult() and obviously this is why none of your guys methods worked as onCreateView wasn't called this time. I have change new MyDialogFragment() to the previously initialized dialog fragment that I am actually displaying and everything works now. And I will close this question.

2条回答
SAY GOODBYE
2楼-- · 2019-08-01 13:32

When you start your activity before that try to use this.

intent.putExtra("return-data", true);
查看更多
SAY GOODBYE
3楼-- · 2019-08-01 13:47

Check if the request code is the correct code fro request. You may requesting another code

查看更多
登录 后发表回答