Android应用程序加载页面中事件顺序的帮助



我正在学习安卓系统(具有讽刺意味的是,我是从谷歌和SO的开发者网站交叉而来),但我在早期阶段遇到了麻烦。我要参加的活动顺序是:1.加载启动页2.5秒后(这是暂时的……最终将用于掩盖加载时间),切换到主视图3.在主视图加载上,弹出一个nag窗口(当前为alertDialog),为用户提供两个按钮按压选项

除了一个问题,我所有这些都在工作。启动页面出现时(应用程序启动时),nag窗口立即弹出。您可以看到启动页面在nag窗口下运行良好,它等待5秒,然后切换到main。有人能告诉我我做错了什么吗?在开始页面计数完成之前,我试图让唠叨窗口不弹出?主java页面粘贴在下面:

public class MyProject extends Activity {
    protected Dialog mSplashDialog;
    private static final int NAG_BOX = 0;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyStateSaver data = (MyStateSaver) getLastNonConfigurationInstance();
        if (data != null) {
            // Show splash screen if still loading
            if (data.showSplashScreen) {
                showSplashScreen();
            }
            setContentView(R.layout.main);    
            showDialog(NAG_BOX);
            // Rebuild your UI with your saved state here
        } else {
            showSplashScreen();
            setContentView(R.layout.main);
            // Do your heavy loading here
        }
    }
    @Override
    public Object onRetainNonConfigurationInstance() {
        MyStateSaver data = new MyStateSaver();
        // Save your important data here
        if (mSplashDialog != null) {
            data.showSplashScreen = true;
            removeSplashScreen();
        }
        return data;
    }
    /**
     * Removes the Dialog that displays the splash screen
     */
    protected void removeSplashScreen() {
        if (mSplashDialog != null) {
            mSplashDialog.dismiss();
            mSplashDialog = null;
        }
    }
    /**
     * Shows the splash screen over the full Activity
     */
    protected void showSplashScreen() {
        mSplashDialog = new Dialog(this, R.style.SplashScreen);
        mSplashDialog.setContentView(R.layout.splash);
        mSplashDialog.setCancelable(false);
        mSplashDialog.show();
        // Set Runnable to remove splash screen just in case
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
          @Override
          public void run() {
            removeSplashScreen();
          }
        }, 5000);
    }
    /**
     * Simple class for storing important data across config changes
     */
    private class MyStateSaver {
        public boolean showSplashScreen = false;
        // Your other important fields here
    }
     @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case NAG_BOX:
                // This example shows how to add a custom layout to an AlertDialog
                LayoutInflater factory = LayoutInflater.from(this);
                final View textEntryView = factory.inflate(R.layout.nagbox, null);
                return new AlertDialog.Builder(MyProject.this)
                    .setView(textEntryView)
                    .setNegativeButton("Maybe Later", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                           dismissDialog(NAG_BOX);
                        }
                    })
                    .setPositiveButton("Go To Site", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            Uri url = Uri.parse("http://www.google.com");
                            Intent intent = new Intent(Intent.ACTION_VIEW, url);
                            startActivity(intent);
                        }
                    })
                    .create();
            }
            return null;
        }
}

为什么不在关闭启动屏幕后调用showDialog(NAG_BOX)?

protected void removeSplashScreen() {
    if (mSplashDialog != null) {
        mSplashDialog.dismiss();
        mSplashDialog = null;
        showDialog(NAG_BOX);
    }
}
if (data != null) {
    // Show splash screen if still loading
    if (data.showSplashScreen) {
        showSplashScreen();
    }
    setContentView(R.layout.main);    
    showDialog(NAG_BOX);

在代码的这一部分中,嵌套的if虽然显示了splashscreen,但并不意味着它不会显示NAG_BOX

您需要一个围绕showDialog(NAG_BOX)的条件,该条件在showSplashSreen()函数

之后调用

首选方法是使用AsyncTask进行长操作。您可以使用publishProgress()方法更新启动屏幕以指示进度。这也将为您的假等待时间创建一个很好的存根点。

然后在AsyncTask的onPostExecute()中,您可以显示您的对话框。

http://developer.android.com/reference/android/os/AsyncTask.html

相关内容

  • 没有找到相关文章

最新更新