Mrinal
Mrinal

Reputation: 108

ActionbarSherlock menuitem setActionView(null) does not stop rotating

Let me give you a brief scenario what is happening.

I want to have a refresh button on my action bar which switches to the indeterminateProgressStyle (a rotating circle) while somethings are loading.

In my project i have the following layouts. (e.g. res/layout/actionbar_indeterminate_progress.xml)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center">
    <ProgressBar android:layout_width="32dp"
        android:layout_height="32dp"
        android:layout_marginLeft="12dp"
        android:layout_marginRight="12dp"
        android:layout_gravity="center"
        style="?android:attr/indeterminateProgressStyle" />
</FrameLayout>

and when the user clicks on the refresh button I do the following.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);

    refreshMenuItem = menu.findItem(R.id.menu_refresh);

    if (refreshMenuItem != null) {
        // Show refresh button if we are in action bar mode, but only if
        // there's something to be refreshed
        boolean selectedItemExists = (getCurrentBrowsingContainer() != null);
        refreshMenuItem.setVisible(selectedItemExists);
        if (is_spinner_running) {
            refreshMenuItem.setActionView(R.layout.actionbar_indeterminate_progress);
        } else {
            refreshMenuItem.setActionView(null);
        }
    }

    return super.onCreateOptionsMenu(menu);
}

also my res/menu/main.xml looks like below.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    ...
        <item android:id="@+id/menu_refresh" android:icon="@drawable/ic_menu_refresh" android:showAsAction="always" android:title="@string/refresh" />
    <item android:id="@+id/menu_search" android:title="@string/menu_search" android:icon="@android:drawable/ic_menu_search" android:showAsAction="never" />
    <item android:id="@+id/settings" android:title="@string/settings" android:icon="@android:drawable/ic_menu_preferences"></item>
    <item android:id="@+id/exit" android:title="@string/exit" android:icon="@android:drawable/ic_menu_close_clear_cancel"></item>   
        ...
</menu>

The problem is that, I start my activity in portrait for example and hit on the refresh button, it stops nicely. Now i rotate it and hit the refresh button again but now it keeps on showing the indeterminate progress for some reason. I checked, the refreshMenuItem.setActionView(null) is getting called but it still keeps on showing the indeterminate thingy. Also now the refreshMenuItem seems to be invisible for some reason, cause the onOptionsItemSelected is not getting called when i click on the indetermintate progress.

so I tried with a different sample app and it works perfectly without any issues. So i am wondering where could i go wrong. any pointers will help.

Upvotes: 0

Views: 1858

Answers (2)

Mrinal
Mrinal

Reputation: 108

Actually I figured out the problem. my project was supposed to handle configuration changes before (so we had configchanges in the manifest). with the actionbarsherlock, it becomes unresponsive with the configchanges.

Now since we were not recreating the activity before on config changes, we were passing the activity in some of our code, which was fine since it was never recreated and we took care of cleaning it. but now to get actionbarsherlock working we have to recreate the activity on configchanges. The earlier instance of the activity was still present in some parts of the code and was not getting overridden with the new instance of the activity. so the menuitem which i was trying to stop spinning was actually belonging to the older instance and not the new one.

so refactored the code to use the current instance of the activity and everything worked great!

Upvotes: 1

Y2i
Y2i

Reputation: 3868

Regarding onOptionsItemSelected not getting called when clicking on the intermediate progress: when action view is set, it disables the default behavior of the action item. It also overrides previously set action provider. The only way to handle action view click is by setting OnClickListener.

Regarding intermediate progress not stopping after rotation: I did not have exact this problem. In my case the intermediate progress would not show up at all when I click on the refresh action item, but it would show up nicely if it was triggered from the code. The way I made it work is by moving the code that calls setActionView(...) to Handler.post(Runnable).

Upvotes: 0

Related Questions