显示加载活动,直到主要活动完全加载

时间:2017-08-21 08:07:37

标签: java android android-activity loading progressdialog

以下是在 onCreate 方法完全执行后要加载的主要活动的代码。
提到This for closing one activity from another

public class DictionarySscWords extends AppCompatActivity implements View.OnClickListener, TextToSpeech.OnInitListener 
{
protected void onCreate(Bundle savedInstanceState) 
 {
  super.onCreate(savedInstanceState);
  context = this;
  Intent myIntent = new Intent(DictionarySscWords.this,LoadingIt.class);
  startActivity(myIntent);
  setContentView(R.layout.activity_main);
  //all this activity work
  LoadingIt.fa.finish(); //close Loading activity
 }
}

现在这里是我的loadingIt活动的代码

public class LoadingIt extends AppCompatActivity {
Context context;
public static LoadingIt fa;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_loading_it);
    context=this;
    fa = this;
    ProgressDialog dialog=new ProgressDialog(context);
    dialog.setMessage("Loading Please wait!!");
    dialog.setCancelable(false);
    dialog.setInverseBackgroundForced(false);
    dialog.show();
}
}

问题是 LoadingIt 活动永远不会完成且应用程序停留在加载屏幕上我想完成此活动,因为先前的活动 onCreate 方法已完全执行 感谢

2 个答案:

答案 0 :(得分:0)

我觉得你的解决方案并不是最好的做法。这就是我要做的事情:

  1. LoadingIt活动声明为启动活动

  2. onCreate方法中,加载后台所需的资源(例如AsyncTaskIntentService

  3. 加载完成后,完成LoadingIt活动并显示DictionarySscWords活动

  4. 如果您发布加载逻辑,我可以提供示例实现。

答案 1 :(得分:0)

你可以在LoadingIt类的onCreate()中使用Thread

public class LoadingIt extends AppCompatActivity {

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);
      getSupportActionBar().hide();
      setContentView(R.layout.activity_loading_it);

      Thread loadingThread = new Thread() {

        @Override
        public void run() {
            try {
                super.run();
                sleep(2000);
            } catch (Exception e) {

            } finally {

                Intent main = new Intent(LoadingIt.this,MainActivity.class);
                startActivity(main);
                finish();
            }
        }
    };
    loadingThread.start();
}
相关问题