onPostExecute回调没有调用invalidateOptionsMenu

时间:2014-10-18 20:00:52

标签: android

我有一个活动,当我点击刷新操作栏图标时,它会启动异步任务。我在mainactity中有异步任务的回调。在preexecute我正在操作栏上显示进度条,在postexecute我显示后退刷新项目。但是当异步任务启动后有屏幕旋转时,onpostexecute会运行,但它不会调用invalidate options菜单。

请帮帮我。

这是我的MainActivity:



package in.cdac.enbee;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity implements RefreshTask.TaskCallbacks {

	// Our created menu to use
    private Menu mymenu;
	volatile static boolean isrefreshing=false;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	Log.d("Debug", "OncreateOptionsMenu");
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        
        // We should save our menu so we can use it to reset our updater.
        mymenu = menu;
        
        if(isrefreshing) {
        	 menu.findItem(R.id.action_refresh).setActionView(R.layout.action_progressbar);
		} else {
			menu.findItem(R.id.action_refresh).setActionView(null);;
		}
        return true;
    }
    
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
    	
        switch(item.getItemId()) {
        case R.id.action_refresh:
        	// Do animation start
            new RefreshTask(this).execute();
            return true;
        case R.id.action_settings:
        	return true;
        }
        
        return super.onOptionsItemSelected(item);
    }
    
    
    @Override
    protected void onSaveInstanceState (Bundle savedInstanceState) {
    	//Log.d("Debug", "OnSave");
    	//Always call superclass first
    	super.onSaveInstanceState(savedInstanceState);
    	// Restore value of members from saved state
        //savedInstanceState.putBoolean("isRefreshing", isrefreshing);
        
    }
    
    @Override
    protected void onRestoreInstanceState (Bundle savedInstanceState) {
    	//Log.d("Debug", "OnRestore");
    	// Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            // Restore value of members from saved state
        	//isrefreshing = savedInstanceState.getBoolean("isRefreshing");
        } else {
            // Probably initialize members with default values for a new instance
        	//isrefreshing = false;
        }
           
    }   
    
	@Override
	public void onPreExecute() {
		// TODO Auto-generated method stub
		isrefreshing = true;
		invalidateOptionsMenu();
	}


	@Override
	public void onCancelled() {
		// TODO Auto-generated method stub
		isrefreshing = false;
		invalidateOptionsMenu();
	}


	@Override
	public void onPostExecute(Boolean done) {
		// TODO Auto-generated method stub
   		isrefreshing = false;
		
		if(done) {
    		Toast.makeText(this, "Done refreshing", Toast.LENGTH_SHORT).show();
    	} else {
    		Toast.makeText(this, "Downloading Failed", Toast.LENGTH_SHORT).show();
    	}
		invalidateOptionsMenu();
	}
    
}




和我的异步任务:



package in.cdac.enbee;

import android.os.AsyncTask;

public class RefreshTask extends AsyncTask<Void, Void, Boolean> {
	 
    /**
	 * 
	 */
    
    static interface TaskCallbacks {
		void onPreExecute();
		void onCancelled();
		void onPostExecute(Boolean done);
	}
    
    private TaskCallbacks mCallbacks;
      
    public RefreshTask(TaskCallbacks mCallbacks) {
		this.mCallbacks = mCallbacks;
	}
    
    @Override
	protected void onPreExecute() {
		mCallbacks.onPreExecute();
		
	}

    @Override
    protected void onCancelled() {
      if (mCallbacks != null) {
        mCallbacks.onCancelled();
      }
    }
    
    @Override
    protected Boolean doInBackground(Void... nope) {
        try {
            // Set a time to simulate a long update process.
            Thread.sleep(4000);
             
            return true;
             
        } catch (Exception e) {
            return false;
        }
    }
     
    
    @Override
	protected void onPostExecute(Boolean done) {
		if (mCallbacks != null) {
			mCallbacks.onPostExecute(done);
		}
	}

}
&#13;
&#13;
&#13;

和我的进度条布局文件:

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<ProgressBar	xmlns:android="http://schemas.android.com/apk/res/android"
        		android:id="@+id/progressBar"
        		android:layout_width="wrap_content"
     			android:layout_height="wrap_content">
</ProgressBar>
&#13;
&#13;
&#13;

谢谢。

2 个答案:

答案 0 :(得分:0)

您正在将Acitvity对象传递给AsyncTask。这种方法不起作用。基本上,android系统可以随时销毁并重新创建你的活动(例如,当方向发生变化时),你必须为这种情况做好准备。

您现在看到的是您的代码正在被调用,但它使用的是旧活动,而不是在方向更改后创建的活动,因此您的代码无效。

答案 1 :(得分:0)

我不知道这是否是正确的方法,但我确实有一些解决方法并且有效。

我在mainactivity中创建了对RefreshTask Async任务的引用。最初使其为空。

static RefreshTask rt=null;

然后在异步任务类中添加了一个方法。

public void updateobject(TaskCallbacks mCallbacks) {
    	this.mCallbacks = mCallbacks;
    }

并且在mainactivity中,在onRestoreInstanceState中,调用该方法。

@Override
    protected void onRestoreInstanceState (Bundle savedInstanceState) {
    	//Log.d("Debug", "OnRestore");
    	// Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);
        if(rt != null) {
        	rt.updateobject(this);
        }
           
    }

请让我知道你的想法。 另外,建议我采用另一种方法。