按下后退按钮并转到上一个活动时如何控制标题中的不确定进度条

发布于 2024-12-05 03:35:43 字数 2826 浏览 1 评论 0原文

因此,我尝试在自定义标题标题中使用不确定的进度条来显示任何后台工作。现在,使用 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:

  1. 启动异步任务
  2. 显示进度条
  3. 执行后台工作

转到活动 B:

  1. 进度条仍然显示
  2. 结束后台工作
  3. 隐藏进度条*正确的行为< /strong>

返回活动 A:

  1. 进度条可见*不正确的行为 ...

我在 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:

  1. launches asynctask
  2. shows progressbar
  3. does background work

go to Activity B:

  1. progressbar still shows
  2. ends background work
  3. hides progressbar *correct behaviour

go back to Activity A:

  1. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦太阳 2024-12-12 03:35:43

这是我尝试过的方法:

@Override
public void onRestart () 
{
    super.onRestart();
    if (getThemeId() != R.style.CustomThemeNoHeader)
    {
    Toast.makeText(this, "visible(0): " + ProgressBar.VISIBLE+" : Current :" + getProgressBarstate(),Toast.LENGTH_LONG).show();

    // doesnt work
    QuadrosMobileActivity.progressHeader.setVisibility(getProgressBarstate());

    // works
    ((TextView)findViewById(R.id.header_title)).setText("Tretas");
    }
}

This the method i tried:

@Override
public void onRestart () 
{
    super.onRestart();
    if (getThemeId() != R.style.CustomThemeNoHeader)
    {
    Toast.makeText(this, "visible(0): " + ProgressBar.VISIBLE+" : Current :" + getProgressBarstate(),Toast.LENGTH_LONG).show();

    // doesnt work
    QuadrosMobileActivity.progressHeader.setVisibility(getProgressBarstate());

    // works
    ((TextView)findViewById(R.id.header_title)).setText("Tretas");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文