Issue
I've been getting this weird thing in my app.
I have an AppCompatActivity
, which has ViewPager
inside it, which contains two fragments. I've added LottieAnimationView
inside both the fragments.
In FragmentA
I'm fetching data from server using Retrofit
, and on loading, it hides the LottieAnimationView
. In FragmentB
(which is only for purpose of checking what's wrong), I'm just using Handler
to hide the LottieAnimationView
after 3 seconds.
Now, whenever I minimize the app, and then open it again, I see the LottieAnimationView
that I setVisibility(View.GONE)
only in FragmentA
. In FragmentB
, I do not see the View when I setVisibility(View.GONE)
after minimizing and opening app again (which works as expected).
Here's the image of what I see in FragmentA
.
The LottieAnimationView
is in paused state.
Here's the code to my FragmentA
.
public class FragmentA extends Fragment {
private AdapterEventStore mAdapter;
private List<Item> mStore;
private final String hostName = "https://xxx.xxx.xxx";
private View view;
private Context context;
private LottieAnimationView lottieAnimationView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_a, container, false);
context = view.getContext();
lottieAnimationView = view.findViewById(R.id.ais_lav_loading);
lottieAnimationView.setVisibility(View.VISIBLE);
initRecyclerView();
return view;
}
private void initRecyclerView() {
RecyclerView store = view.findViewById(R.id.ais_rv_event_store);
store.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
mStore = new ArrayList<>();
mAdapter = new AdapterEventStore(context, mStore);
store.setAdapter(mAdapter);
fetchDataFromServer();
}
private void fetchDataFromServer() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
final Retrofit retrofit = new Retrofit.Builder()
.client(httpClient.build())
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(hostName)
.build();
APIGetItem itemShop = retrofit.create(APIGetItem.class);
Call<ModelEventExclusive> call = itemShop.getEventStore(hostName);
call.enqueue(new Callback<ModelEventExclusive>() {
@Override
public void onResponse(Call<ModelEventExclusive> call, Response<ModelEventExclusive> response) {
Log.e("Response: ", response.body().getItems().get(0).getItemName());
mEventStore.addAll(response.body().getItems());
mAdapter.notifyDataSetChanged();
hideLottieAnimation();
}
@Override
public void onFailure(Call<ModelEventExclusive> call, Throwable t) {
hideLottieAnimation();
Toast.makeText(context, "Error:"+t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
Log.e("Error occurred: ", t.getMessage());
}
});
}
private void hideLottieAnimation(){
lottieAnimationView.cancelAnimation();
lottieAnimationView.setVisibility(View.GONE);
}
}
Layout for FragmentA
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/ais_rv_event_store"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.airbnb.lottie.LottieAnimationView
android:id="@+id/ais_lav_loading"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_loop="true"
android:visibility="gone"
app:lottie_repeatMode="reverse"
app:lottie_rawRes="@raw/loading" />
</FrameLayout>
Please note that the Visibility of LottieAnimationView
is set to GONE
at first.
The above code to fetch items 100% works, please don't point out any errors in syntax/variable names as I've done bad job at trying to hide the actual variable/class names.
Here's the code to FragmentB
public class FragmentB extends Fragment {
private LottieAnimationView anim_loading;
private View view;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_b, container, false);
anim_loading = view.findViewById(R.id.lav_loading);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
anim_loading.cancelAnimation();
anim_loading.setVisibility(View.GONE);
}
}, 3000);
return view;
}
}
Can you please tell what I'm doing wrong here? Why it works as expected in one fragment and doesn't in another? Does it have to do anything with how I'm hiding it inside Retrofit callback?
Solution
The problem is that, when you get form the background, the onResume
gets called, so you need to add the line to hide the view in onResume
, not onCreateView
.
Check out the Android Developer page on fragments for more details.
Answered By - Sayed El-Abady
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.