[英]Activity and AlertDialog are being launched twice
我有顯示兩次的AlertDialog
,我也不知道為什么! 這個問題並不僅僅適用於AlertDialogs
; 但是,它也適用於"Activities"
。
請注意,我面臨Android 4.0.3。的問題。但是,當我在Android 2.3.6上運行該應用程序時,一切正常。
為了解決Activitie
的問題,我在清單文件中進行了設置: android:launchMode="singleInstance"
並且它可以正常工作。
但是,對於AlertDialog
,無法做到這一點,因為它們在清單文件中沒有任何引用來將其設置為singleInstance
或類似的東西。
有人告訴我放置一個BreakPoint
以便在show()
之后檢查我的代碼。 但是我不知道如何放置一個BreakPoint
以及如何檢查。
編輯:
我正在使用HoloEverywhere和SherlockActionBar。 我不知道他們有多少影響。
@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
switch(item.getItemId())
{
case R.id.action_one:
alertDialog();
break;
case R.id.action_two:
Intent i = new Intent(this,Info.class);
startActivity(i);
overridePendingTransition(0, 0);
break;
}
return super.onOptionsItemSelected(item);
}
private void alertDialog(){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("My title");
AlertDialog alert= builder.create();
alert.show();
}
case R.id.action_two
則在清單文件中設置launchMode="singleInstance"
后,它可以正常工作。 但是, case R.id.action_one
啟動AlertDialog
它仍然會打開兩次。
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
// TODO Auto-generated method stub
com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
inflater.inflate(R.menu.action_options, menu);
return super.onCreateOptionsMenu(menu);
}
更改此行:
return super.onCreateOptionsMenu(menu);
至:
return true;
當您調用super.onCreateOptionsMenu
,它將附加的onMenuItemClickListener
偵聽器附加到菜單中的每個項目,這樣將導致記錄2次單擊。
(PS:我實際上已經弄清楚了,打算將其發布,但是必須打個電話。不要開玩笑)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.