hemantv hemantv - 1 month ago 22
Android Question

Snackbar in a particular fragment

I have 5 sliding tabs in a

ViewPager
, each holding a different fragment. If the data fails to load, I want to notify the user via Snackbar. It's working fine.

What I'm trying to achieve is, if data fails to load in a particular fragment, I want to show a
Snackbar
in that particular fragment only. Is it possible?


I'll elaborate:


Consider a sliding tab layout with 3 fragments as A, B, & C. All 3 fragments perform some network query and load the data. If due to some reason data fails to load in fragment A, a snackbar should be shown indefinitely. And if the user slides to fragment B (in which data loaded successfully), snackbar should not be visible.

So the snackbar should only be visible in the fragment which failed to load data.


Fragment Code:


<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_latest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground"
tools:context=".MainFragment">

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/date_layout"
android:visibility="gone"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/country_select"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/country"
/>

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/month_select"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/month"
/>

<android.support.v7.widget.AppCompatSpinner
android:id="@+id/year_select"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/year"
/>

</LinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/latest_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

</android.support.v4.widget.SwipeRefreshLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">

<TextView
android:id="@+id/not_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:textSize="16sp" />

</LinearLayout>





Snackbar Code:


if (Utils.isNetworkAvailable(getActivity())) {
adapter.notifyDataSetChanged();
loadData();
} else {
swipeRefreshLayout.setRefreshing(false);
textView.setText(getString(R.string.check_internet));
Snackbar.make(frameLayout, "Loading failed.", Snackbar.LENGTH_INDEFINITE).setAction("Retry", new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity) getActivity()).loadAds();
refreshData();
}
}).show();
}



where


FrameLayout frameLayout = (FrameLayout) v.findViewById(R.id.fragment_latest);

N J N J
Answer

What you can do is override setUserVisibleHint and store snakbar in global variable in fragment when fragment become invisible dismiss the snackbar by using below method

@Override
public void setUserVisibleHint(boolean visible)
{
    super.setUserVisibleHint(visible);
        if (!visible && snackbar != null ){
            snackbar.dismiss()
        }
}