Issue
In my app I get data from the user in a fragment
and use the entered data to get result by JSON
using AsyncTask
then save it to database
then display it in another fragment
.
The problem is when I execute the asyncTask
and open the fragment
the data doesn't show unless I rotate the device (force fragment recreation).
My first fragment code:
...
SecondFragment secondFragment = new SecondFragment();
new SecondFragment.GetTask("s").execute(URL);
FragmentTransaction ft =getActivity().getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_up, R.anim.slide_in_up);
ft.add(R.id.s_con, secondFragment).addToBackStack("back").commit();
...
Second Fragment code:
...
recyclerView.setAdapter(new SAdapter(getContext(),R.layout.group_header));
...
public static class GetTask extends AsyncTask<String, String, Void> {
String arrayname;
GetTask(String arrayname) {
this.arrayname = arrayname;
}
}
@Override
protected Void doInBackground(String... params) {
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
String finalJson = buffer.toString();
JSONObject parentObject = new JSONObject(finalJson);
JSONObject secondParentObject = parentObject.getJSONObject("data");
//json getter and adder to database
JSONArray Array = secondParentObject.getJSONArray(arrayname);
for (int i = 0; i < Array.length(); i++) {
JSONObject finalObject = Array.getJSONObject(i);
switch (arrayname) {
case "s":
db.SAddJson(finalObject);
break;
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
}
Solution
AsyncTask executes in another thread. Second fragment is not updated because the asynctask was still executing when you loaded it. When you changed orientation it simple recreated second fragment by then Asynctask finished its task that's why it got updated. Move AsyncTask to first fragment and load the 2nd fragment in the Async task's onPostExecute(). onPostExecuted() runs in the UI thread.
When using AsyncTask inside a fragment, there might be scenarios like orientation change where the activity will get recreated and this can cause your fragment to be recreated as well and the AsyncTask will be called again. This can also cause memory leak. To avoid this call the method inside your onCreate() of first Fragment;
setRetainInstance(true);
This will not kill the fragment on orientation change and it will be safer. To know more about handling orientation change check out this link:
https://developer.android.com/guide/topics/resources/runtime-changes.html
Answered By - Harikumar Alangode
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.