[英]Help with order of events in Android app loading page
我正在學習 Android(具有諷刺意味的是來自 Google 和 SO 的開發人員網站的交叉),我在早期步驟中遇到了麻煩。 我要去的事件順序是: 1. 加載啟動頁面 2. 5秒后(這是暫時的……最終會被用來掩蓋加載時間),切換到主視圖 3. 在主視圖加載,彈出一個 nag window(當前是一個 alertDialog),它為用戶提供了兩個按鈕按下選項
除了一個問題,我所有這些都正常工作。 當啟動頁面出現時(當應用程序啟動時),nag window 會立即彈出。 您可以看到啟動頁面在 nag window 下方運行良好,它等待 5 秒,然后切換到主頁面。 有人可以告訴我我做錯了什么,就試圖讓嘮叨 window 在啟動頁面計數完成之前不彈出? 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);
在您的代碼的這一部分中,您的嵌套 if 雖然顯示了您的啟動畫面,但這並不意味着它不會顯示 NAG_BOX
您需要在 showSplashSreen() function 之后調用的showDialog(NAG_BOX)
周圍的條件
為什么在您關閉啟動屏幕后不調用 showDialog(NAG_BOX) ?
protected void removeSplashScreen() {
if (mSplashDialog != null) {
mSplashDialog.dismiss();
mSplashDialog = null;
showDialog(NAG_BOX);
}
}
首選方法是使用 AsyncTask 進行長操作。 您可以使用 publishProgress() 方法更新初始屏幕以指示進度。 這也將為您的虛假等待時間創建一個很好的存根點。
然后在 AsyncTask 的 onPostExecute() 中,您可以顯示您的對話框。
http://developer.android.com/reference/android/os/AsyncTask.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.