José Emanuel
José Emanuel

Reputation: 11

Android Studio - Bottom navigation crash when changing between fragments

I am creating and app and so far everything was good. Today I was dealing with the data in a fragment to reach my webservice and return the json and then insert that data into my sqlite and after that show it in a listview and update the data second by second.

Everything was right until i find that if I click too fast in the bottom navigation where the items from the fragments are, it will crash the aplication while it is doing the transaction on the fragment. it only occurs when I click between that fragment and more 1 of the other 4.

So my question is, is there anyway I can deal with this problem? like if I click in another item from bottom navigation the transitions from the last item stops? if so how?

Upvotes: 0

Views: 2029

Answers (2)

José Emanuel
José Emanuel

Reputation: 11

I found that 'compile 'com.android.volley:volley:1.0.0'' version of volley had this error and google (or the people that made the volley) didn't update volley in google library since then.

The way to work around this was work with a non-official version: 'compile 'com.mcxiaoke.volley:library:1.0.19''

This version had this "bugs" from volley already corrected. For the future if anyone wanna know more about this there is the link from mcxiaoke: https://github.com/mcxiaoke/android-volley

and a link to how to use it: https://www.thorntech.com/2016/03/parsing-json-android-using-volley-library/

Upvotes: 0

Srikar Reddy
Srikar Reddy

Reputation: 3706

As far as I can understand (with little info), the issue is mostly likely that the fragment is getting destroyed (when the code logic is trying to connect to the server and return the json data to the app) and you're trying to access the destroyed fragment elements (or items, here in this case, list view, to populate it with the received data).

Inside your each and every fragment, after the json data is received and before doing anything else, check if the fragment is still visible and attached to the activity and also the activity is not destroyed.

// Check the activity is not destroyed and the 
// fragment is still connected to the activity
if (getActivity() != null && isAdded()) {
    // Parse the JSON data
    // Write to your SQL database
    // Load the data into the list view
}

Upvotes: 3

Related Questions