简体   繁体   English

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

[英]Help with order of events in Android app loading page

I'm learning Android (ironically from a cross of the developer site from Google and SO), and I'm having trouble with an early step.我正在学习 Android(具有讽刺意味的是来自 Google 和 SO 的开发人员网站的交叉),我在早期步骤中遇到了麻烦。 The order of events I am going for is: 1. Load splash page 2. After 5 seconds (this is temporary... will eventually be actually used to cover up load time), switch to main view 3. On the main view load, pop a nag window (currently an alertDialog), which gives the user two button press options我要去的事件顺序是: 1. 加载启动页面 2. 5秒后(这是暂时的……最终会被用来掩盖加载时间),切换到主视图 3. 在主视图加载,弹出一个 nag window(当前是一个 alertDialog),它为用户提供了两个按钮按下选项

I have this all working except for ONE problem.除了一个问题,我所有这些都正常工作。 The nag window pops immediately when the splash page comes up (when the app starts).当启动页面出现时(当应用程序启动时),nag window 会立即弹出。 You can see the splash page is working fine beneath the nag window, it waits 5 seconds, then switches to main.您可以看到启动页面在 nag window 下方运行良好,它等待 5 秒,然后切换到主页面。 Can someone please tell me what I'm doing wrong as far as trying to get the nag window to NOT pop until the splash page count is done?有人可以告诉我我做错了什么,就试图让唠叨 window 在启动页面计数完成之前不弹出? Main java page is pasted below: 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;
        }
}
if (data != null) {
    // Show splash screen if still loading
    if (data.showSplashScreen) {
        showSplashScreen();
    }
    setContentView(R.layout.main);    
    showDialog(NAG_BOX);

In this portion of your code your nested if, although shows your splashscreen, does not mean it will not show the NAG_BOX在您的代码的这一部分中,您的嵌套 if 虽然显示了您的启动画面,但这并不意味着它不会显示 NAG_BOX

You need a condition around the showDialog(NAG_BOX) which is called after the showSplashSreen() function您需要在 showSplashSreen() function 之后调用的showDialog(NAG_BOX)周围的条件

Why not invoke showDialog(NAG_BOX) after you dismiss your splash screen?为什么在您关闭启动屏幕后不调用 showDialog(NAG_BOX) ?

protected void removeSplashScreen() {
    if (mSplashDialog != null) {
        mSplashDialog.dismiss();
        mSplashDialog = null;
        showDialog(NAG_BOX);
    }
}

The preferred method is to use AsyncTask for long operations.首选方法是使用 AsyncTask 进行长操作。 You can use the publishProgress() method to update the splash screen to indicate progress.您可以使用 publishProgress() 方法更新初始屏幕以指示进度。 This will also create a nice stub spot for your fake wait time.这也将为您的虚假等待时间创建一个很好的存根点。

Then in AsyncTask's onPostExecute() you can show your dialog.然后在 AsyncTask 的 onPostExecute() 中,您可以显示您的对话框。

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

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

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