Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to determine in Android if an Activity's content view has been created/displayed?

At the very beginning of my app I load up some Fragments in tabs. Here is one that I load:

WishlistFragment wishlistfragment = new WishlistFragment();

Once the Fragment has been loaded and displayed, I will be calling this from other Fragments:

wishlistfragment.adapter.notifyDataSetChanged();

The problem is, if I call this BEFORE the wishlistfragment is ever created/displayed, I get the following exception:

java.lang.IllegalStateException: Content view not yet created.

Is there a way, programmatically, to determine if a view has been created yet?

like image 328
Ethan Allen Avatar asked Jan 15 '23 03:01

Ethan Allen


1 Answers

I don't know of a built-in way, but you could devise your own by making a method to handle the notification and overriding onCreateView in your WishlistFragment. Something like this might work:

public class WishlistFragment extends ListFragment {
    private boolean _viewExists = false;

    @Override
    public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle bundle )
    {
        _viewExists = true
        return super.onCreateView( inflater, container, bundle );
    }

    public void notifyChange()
    {
        if ( _viewExists )
            getListAdapter().notifyDataSetChanged();
    }
like image 99
Ralgha Avatar answered Jan 17 '23 17:01

Ralgha