0

Within my Fragment I need to add refresh button to my actionbar if network is unreacheble.

For example:

    private void beginRetriveData() {

            final StringBuilder Request = "some_url_here"

            if(!isNetworkAvailable()) {     

                Toast.makeText(getSherlockActivity(), "No active internet connection", Toast.LENGTH_LONG).show(); 
// I NEED TO ADD REFRESH BUTTON TO ACTIONBAR    

            } else {
                     //AsyncTasc here

            }   
        }

Which is the best way to do that ?

I tried the following:

public MenuItem mRefreshMenuItem;

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        getSupportMenuInflater().inflate(R.menu.main, menu);

        mRefreshMenuItem = menu.findItem(R.id.navigation_refresh);
        mRefreshMenuItem.setVisible(false);

        return true;
    }

    @Override
    public void onNetworkUnavailable()
    {
        mRefreshMenuItem.setVisible(true); //ERROR HERE 
    }

Then in Fragment I called:

 if(getActivity() instanceof FragmentsHolderActivity){
      FragmentsHolderActivity myactivity = (FragmentsHolderActivity) activity;
      myactivity.onNetworkUnavailable();
 }

but when I call onNetworkUnavailable(),there was a NullPointerException .

Aniruddha K.M
  • 7,361
  • 3
  • 43
  • 52
Sever
  • 2,338
  • 5
  • 35
  • 55
  • Create a callback to your activity, here is an example: http://stackoverflow.com/questions/14213947/onattach-callback-from-fragment-to-activity – cYrixmorten Nov 03 '13 at 21:27

1 Answers1

3

Override onCreateOptionsMenu() in your Activity and add a refresh MenuItem to the Menu (you can do this by inflating a menu resource or instantiating programmatically). Then make the refresh item invisible and save it as an instance variable to access later.

Add to your Activity:

private MenuItem mRefreshMenuItem;

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // add your refresh button to res/menu/main.xml
    getMenuInflater().inflate(R.menu.main, menu);

    mRefreshMenuItem = menu.findItem(R.id.refresh);
    mRefreshMenuItem.setVisible(false);

    return true;
}

Next, add an interface so your Fragment can let your Activity know when the network error occurs as described in Communicating with Other Fragments.

Add to your Fragment:

private OnNetworkUnavailableListener mListener;

public interface OnNetworkUnavailableListener
{
    public void onNetworkUnavailable();
}

@Override
public void onAttach(Activity activity)
{
    super.onAttach(activity);
    try
    {
        mListener = (OnNetworkUnavailableListener)activity;
    }
    catch (ClassCastException ex)
    {
        throw new ClassCastException(activity.getClass().getSimpleName() + " must implement OnNetworkUnavailableListener");
    }
}

Additionally, call the method on your interface when the network is unavailable:

private void beginRetriveData()
{
    ...
    if(!isNetworkAvailable())
    {     
        mListener.onNetworkUnavailable();
    }
    ...
}

Finally, implement your interface in the Activity and set the MenuItem's visibility to visible when the network error occurs. Remember to add the "implements OnNetworkUnavailableListener" to your Activity.

public class MainActivity extends Activity implements OnNetworkUnavailableListener
{
    ...

    @Override
    public void onNetworkUnavailable()
    {
        mRefreshMenuItem.setVisible(true);
    }
}
jhotovy
  • 116
  • 3
  • Thank you. But not work. Error (NullPointerException) occurred when I call mListener.onNetworkUnavailable() on line with mRefreshMenuItem.setVisible(true); – Sever Nov 03 '13 at 22:31
  • In your updated example, you may be calling onNetworkUnavailable() before onCreateOptionsMenu() is called. When are you calling onNetworkUnavailable()? – jhotovy Nov 04 '13 at 01:28
  • @onCreateOptionsMenu, You are right, onCreateOptionsMenu called after oncreate(), and my fragment called in oncreate. So my trouble is call fragment after onCreateOptionsMenu. But I dont know how. – Sever Nov 04 '13 at 05:40
  • Try calling it in onStart() instead of onCreate(). – jhotovy Nov 04 '13 at 07:18