简体   繁体   English

android 工作室活动开始前完成前一

[英]android studio activity starts before finishing prev one

I m calling prog() function in the Mainactivity which performs loading page using progress bar and I call just after the prog function LoginActivity.我在 Mainactivity 中调用prog() function,它使用进度条执行加载页面,我在 prog function LoginActivity 之后调用。 BUT, it calls login activity before exciting prog function, I am new at the android studio and need your help.但是,它在令人兴奋的程序 function 之前调用登录活动,我是 android 工作室的新手,需要您的帮助。 here is my code这是我的代码

public class MainActivity extends AppCompatActivity {

ProgressBar pb;
int counter = 0;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = findViewById(R.id.textView);

    prog();//loading page
     Intent intentLoginPage = new Intent(MainActivity.this,LoginActivity.class);
     startActivity(intentLoginPage);

}

public boolean prog(){// to dispay progress bar as loading bar in loading page

    pb = findViewById(R.id.progressBar);
    final Timer t = new Timer();
    TimerTask tt = new TimerTask() {
        @Override
        public void run(){
            counter++;
            textView.setText(counter + "%");//0 to 100%
            pb.setProgress(counter);
            if(counter == 100)
                t.cancel();

        }
    };
    t.schedule(tt,0,60);
    return true;
}
}

May be you need call startActivity in run()?:可能您需要在 run() 中调用 startActivity 吗?:

TimerTask tt = new TimerTask() {
        @Override
        public void run(){
            counter++;
            textView.setText(counter + "%");//0 to 100%
            pb.setProgress(counter);
            if(counter == 100) {
                t.cancel();
                Intent intentLoginPage = new Intent(MainActivity.this,LoginActivity.class);
                startActivity(intentLoginPage);
            }

        }
    };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM