更新操作栏菜单项,由于AsyncTask的结果无延迟 [英] Updating Action Bar Menu Item due to AsyncTask result without delay

查看:125
本文介绍了更新操作栏菜单项,由于AsyncTask的结果无延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个应用程序,它提供了一个详细视图活动(上一个列表项点击可显示更多的信息时,这就是所谓的一个活动)。这个细节活动将显示一个最爱开始在操作栏(它是黄色的,如果该项目是最爱透明如果不是 - 用于这个目的,我有不同的图像资源)

I developed an Application which offers a detail view Activity (An activity which is called when clicking on a list item to display further information). This details Activity shall display a "favorite" start in the Action Bar (it is yellow if the item is a favorite, transparent if not - for this purpose I have to different image resources)

如果我点击菜单项,该项目将添加/删除表格的用户的最爱:

If i click the menu item, the item is added / removed form the user's favorites:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        ...
    } else if (item.getItemId() == R.id.action_no_favorite) {
        ...
        new FavoriteTask(DetailsActivity.this).execute(user, item);
        mMenu.findItem(R.id.action_favorite).setVisible(true);
        mMenu.findItem(R.id.action_no_favorite).setVisible(false);
    } else if (item.getItemId() == R.id.action_favorite) {
        ...
        new NoFavoriteTask(DetailsActivity.this).execute(user, item);
        mMenu.findItem(R.id.action_favorite).setVisible(false);
        mMenu.findItem(R.id.action_no_favorite).setVisible(true);
    }
    return super.onOptionsItemSelected(item);
}

这工作没有任何问题。

但是,如果用户打开详细视图,在操作栏菜单项需要立即更新,这取决于是否这个项目已经是一个最喜欢与否。我通过重写onCreateOptionsMenu解决了这个问题:

But if the user opens the detail view, the Action Bar menu item needs to be updated immediately, depending on if this item is already a favorite or not. I solved this issue by overriding onCreateOptionsMenu:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.details_activity_actions, menu);
    this.mMenu = menu;
    ...
    new FetchFavortieStatusTask().execute(user, item);
    return super.onCreateOptionsMenu(menu);
}

FavoriteStatusTask提供了一个简单的响应(如果该项目是一个最喜欢与否),工作就像一个魅力。在onPostExecute,我更新了菜单项:

FavoriteStatusTask delivers a simple response (if the item is a favorite or not) and works like a charm. In onPostExecute, I update the menu item:

    @Override
    public void onPostExecute(String result) {
        super.onPostExecute(result);
        if (mMenu != null) {
            if (result.equals("no favorite")) {
                ...
                mMenu.findItem(R.id.action_favorite).setVisible(false);
                mMenu.findItem(R.id.action_no_favorite).setVisible(true);
            } else {
                ...
                mMenu.findItem(R.id.action_favorite).setVisible(true);
                mMenu.findItem(R.id.action_no_favorite).setVisible(false);
            }
        }
    }

这会发生的问题:如果我打开一个列表项,以获得详细信息视图中,图标的〜3秒的延迟后更改或立即如果我触摸屏幕。延迟是definetly不是由的AsyncTask ,结果被交付方式较快所致。如果我执行FetchFavortieStatusTask()在活动的onCreate方法的末尾,它将按预期工作的大部分时间(但仍,有时我确实有这个延迟)。移动的AsyncTask 来在prepareOptionsMenu不会改变任何东西。

The problem that occurs: If I open a list item to get the detail view, the icon is changed after a delay of ~3 seconds or immediately if I touch the screen. The delay is definetly not caused by the AsyncTask, the result is delivered way faster. If I execute FetchFavortieStatusTask() in the end of the Activity's onCreate method, it works as expected most of the time (but still, sometimes I do have this delay). Moving the AsyncTask to onPrepareOptionsMenu doesn't change anything.

推荐答案

CI_的评论是一个很好的提示,但我不得不去适应一个有点不同的方式解决方案(基于这样的回答:Is supportInvalidateOptionsMenu()的工作?

ci_'s comment is a good hint but I had to adapt the solution in a bit different way (based on this answer: Is supportInvalidateOptionsMenu() working?)

我打电话的AsyncTask中的onCreate()现在并在其onPostExecute方法,我只设置一个成员变量mIsFavorite真或假,然后执行supportInvalidateOptionsMenu(),它卡列斯在prepareOptionsMenu第二次(但不是onCreateOptionsMenu!) 。这意味着,我必须更新prepareOptionsMenu菜单中的项目的基础上,mIsFavorite的价值。

I call the AsyncTask in onCreate() now and in its onPostExecute method I only set a member variable mIsFavorite to true or false and then execute supportInvalidateOptionsMenu() which calles onPrepareOptionsMenu a second time (but not onCreateOptionsMenu!). This means that I have to update the menu items in onPrepareOptionsMenu, based on the value of mIsFavorite.

这篇关于更新操作栏菜单项,由于AsyncTask的结果无延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆