按下后退按钮并转到上一个活动时如何控制标题中的不确定进度条
因此,我尝试在自定义标题标题中使用不确定的进度条来显示任何后台工作。现在,使用 asynctask 使用 pre 和 post 方法显示和隐藏进度条:
这是包含所有内容的类:
公共抽象类 QuadrosMobileActivity 扩展了 Activity{
protected static volatile ProgressBar progressHeader = null;
protected static int progressBarstate=ProgressBar.INVISIBLE;
//this method will launch respejcting oncreate logic for each activity
public abstract void initActivity();
public int getProgressBarstate(){
return progressBarstate;
}
public void setProgressBarstate(int state){
progressBarstate=state;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//set theme first for showing correct header
setTheme(getThemeId());
super.onCreate(savedInstanceState);
//in case the screen will have a header
if(getThemeId()!=R.style.CustomThemeNoHeader){
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(getLayoutId());
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_window_title);
//set text header
((TextView)findViewById(R.id.header_title)).setText(getWindowTitle());
progressHeader= (ProgressBar) findViewById(R.id.progresspinner);
progressHeader.setVisibility(getProgressBarstate());
}else {
setContentView(getLayoutId());
}
//execute activity logic
initActivity();
}
private class ProgressBarThread extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
progressHeader.setVisibility(ProgressBar.VISIBLE);
setProgressBarstate(ProgressBar.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
updateResultsInUi();
return null;
}
@Override
protected void onPostExecute(Void result) {
progressHeader.setVisibility(View.INVISIBLE);
setProgressBarstate(View.INVISIBLE);
}
}
} 这是我的主要活动类,我所有的屏幕子活动都从这里延伸出来。 我遇到的问题是,如果我启动异步任务,进度条就会可见(全部在活动 A 中),而如果我转到另一个活动(B),则会发生这种情况,进度条仍然正确显示,并在后台工作结束时隐藏,所有正确的行为。但是,如果我按后退按钮,上一个活动(A)将显示,并且进度条可见。 这是一个简单的图表:
启动活动 A:
- 启动异步任务
- 显示进度条
- 执行后台工作
转到活动 B:
- 进度条仍然显示
- 结束后台工作
- 隐藏进度条*正确的行为< /strong>
返回活动 A:
- 进度条可见*不正确的行为 ...
我在 onResume 方法上尝试过这个:
@Override
protected void onResume() {
super.onResume();
if(getThemeId()!=R.style.CustomThemeNoHeader){
System.out.println("visible: " + ProgressBar.VISIBLE+" : " + getProgressBarstate());
progressHeader.setVisibility(getProgressBarstate());
};
}
但没有效果,我真的可以使用 onResume 来改变按下后退按钮时的进度条状态吗???
So im trying to use a indeterminate progressbar in my custom title header to show any background work. Right now um using an asynctask to show and hide the progressbar using the pre and post methods:
Here's the class that contains everything:
public abstract class QuadrosMobileActivity extends Activity{
protected static volatile ProgressBar progressHeader = null;
protected static int progressBarstate=ProgressBar.INVISIBLE;
//this method will launch respejcting oncreate logic for each activity
public abstract void initActivity();
public int getProgressBarstate(){
return progressBarstate;
}
public void setProgressBarstate(int state){
progressBarstate=state;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
//set theme first for showing correct header
setTheme(getThemeId());
super.onCreate(savedInstanceState);
//in case the screen will have a header
if(getThemeId()!=R.style.CustomThemeNoHeader){
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(getLayoutId());
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_window_title);
//set text header
((TextView)findViewById(R.id.header_title)).setText(getWindowTitle());
progressHeader= (ProgressBar) findViewById(R.id.progresspinner);
progressHeader.setVisibility(getProgressBarstate());
}else {
setContentView(getLayoutId());
}
//execute activity logic
initActivity();
}
private class ProgressBarThread extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
progressHeader.setVisibility(ProgressBar.VISIBLE);
setProgressBarstate(ProgressBar.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
updateResultsInUi();
return null;
}
@Override
protected void onPostExecute(Void result) {
progressHeader.setVisibility(View.INVISIBLE);
setProgressBarstate(View.INVISIBLE);
}
}
}
This is my main activity class where all my screen subactivities extend from.
The problem i have is, if i start the asynctask, the progressbar gets visible(all in Activity A) and while this happens if i go to another activity(B), the progressbar still appears correctly and gets hidden when the background work ends, all correct behaviour. BUT if i press the back button the previous activity(A) shows with the progressbar visible.
Here's a simple diagram:
start Activity A:
- launches asynctask
- shows progressbar
- does background work
go to Activity B:
- progressbar still shows
- ends background work
- hides progressbar *correct behaviour
go back to Activity A:
- progressbar is visible *incorrect behaviour
...
I tried this on the onResume method:
@Override
protected void onResume() {
super.onResume();
if(getThemeId()!=R.style.CustomThemeNoHeader){
System.out.println("visible: " + ProgressBar.VISIBLE+" : " + getProgressBarstate());
progressHeader.setVisibility(getProgressBarstate());
};
}
But to no avail, can i really use the onResume to alter the progressbar state when pressing the back button???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我尝试过的方法:
This the method i tried: