更新操作栏菜单项,由于AsyncTask的结果无延迟菜单项、操作、结果、AsyncTask

由网友(Δ我不帥,但我也不温柔ベ)分享简介:我开发了一个应用程序,它提供了一个详细视图活动(上一个列表项点击可显示更多的信息时,这就是所谓的一个活动)。这个细节活动将显示一个最爱开始在操作栏(它是黄色的,如果该项目是最爱透明如果不是 - 用于这个目的,我有不同的图像资源)I developed an Application which offers a det...

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

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_的评论是一个很好的提示,但我不得不去适应一个有点不同的方式解决方案(基于这样的回答:

阅读全文

相关推荐

最新文章