Java Android应用程序加载页面中事件顺序的帮助
我正在学习Android(讽刺的是,我是从Google和其他开发者网站的交叉处学习的),而我在早期的步骤上遇到了麻烦。我要参加的活动顺序如下: 1.加载启动页 2.5秒后(这是暂时的…最终将用于掩盖加载时间),切换到主视图 3.在主视图加载中,弹出一个nag窗口(当前是一个alertDialog),该窗口为用户提供两个按钮按下选项 除了一个问题外,我所有这些都在工作。启动页面出现时(应用程序启动时),nag窗口立即弹出。您可以看到启动页面在nag窗口下工作正常,它等待5秒钟,然后切换到main。有人能告诉我我做错了什么,在启动页面计数完成之前,我试图让nag窗口不弹出吗?java主页面粘贴如下:Java Android应用程序加载页面中事件顺序的帮助,java,android,user-interface,mobile,Java,Android,User Interface,Mobile,我正在学习Android(讽刺的是,我是从Google和其他开发者网站的交叉处学习的),而我在早期的步骤上遇到了麻烦。我要参加的活动顺序如下: 1.加载启动页 2.5秒后(这是暂时的…最终将用于掩盖加载时间),切换到主视图 3.在主视图加载中,弹出一个nag窗口(当前是一个alertDialog),该窗口为用户提供两个按钮按下选项 除了一个问题外,我所有这些都在工作。启动页面出现时(应用程序启动时),nag窗口立即弹出。您可以看到启动页面在nag窗口下工作正常,它等待5秒钟,然后切换到main。
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虽然显示了splashscreen,但并不意味着它不会显示NAG_框
您需要在showDialog(NAG_框)周围设置一个条件,该条件在showSplashSreen()函数之后调用为什么不在关闭启动屏幕后调用showDialog(NAG_框)
protected void removeSplashScreen() {
if (mSplashDialog != null) {
mSplashDialog.dismiss();
mSplashDialog = null;
showDialog(NAG_BOX);
}
}
首选方法是将AsyncTask用于长操作。您可以使用publishProgress()方法更新初始屏幕以指示进度。这也会为你的假等待时间创建一个很好的存根点 然后在AsyncTask的onPostExecute()中可以显示对话框
我肯定会在接下来的步骤中考虑到这一点,因为这可能是一种比目前更好的方法。我接受了另一个答案,因为它只是让我达到了我需要更快的地方,我想你不能在这个网站上接受多个答案,而且我没有足够的声誉来投票支持答案。我非常感谢您的投入,这将有助于大timeFeel免费制作为已接受:)。虽然我必须同意其他的评论,因为有更干净的方法来完成你所追求的。非常感谢你的快速帮助。我希望有一种像专家交流那样分发积分的方法。你们看起来都很有学识,我会利用我在这里得到的所有信息。这让他马上摆脱困境,但这并不能以一种可接受的方式解决问题。强烈建议不要将任何长时间运行的进程放在UI线程上,它们专门为这些类型的操作提供AsyncTask。尽管如此,您的代码仍然有效。如果我正在推广任何类型的错误行为,请原谅。我习惯于EE,你可以根据相关性接受多个答案和分数。为我学习经验。感谢所有的贡献者。答案正是我在我的文章中所说的。但他们都工作,我们都被否决了?
protected void removeSplashScreen() {
if (mSplashDialog != null) {
mSplashDialog.dismiss();
mSplashDialog = null;
showDialog(NAG_BOX);
}
}