私は自分の共有ダイアログを開くための非常に簡単なアプリケーションを書いています。 XMLレイアウトは1つだけのボタンが含まれます。Androidのダイアログがデバイスを回転させた後に再び開く
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white"
android:gravity="center_horizontal">
<Button android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_margin="20dip"
android:text="Click here to open Share Dialog"
android:onClick="onBtnShareClick"/>
</LinearLayout>
そして活動には、ボタンをクリックすると、私はダイアログ
public class CustomDialog extends Activity {
private static final int SHOW_DIALOG_SHARE = 1;
private ArrayAdapter<ShareItem> mShareAdapter;
@Override
protected void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.custom_dialog);
final ShareItem[] items = {
//new Item("Menu item", R.drawable.icon_assistance),
new ShareItem("Banbe", R.drawable.ic_banbe),
new ShareItem("Facebook", R.drawable.ic_facebook),
new ShareItem("Twitter", R.drawable.ic_twitter),
new ShareItem("Gmail", R.drawable.ic_gmail),
new ShareItem("Other sharing options...", 0)
};
mShareAdapter = new ArrayAdapter<ShareItem>(
this,
android.R.layout.select_dialog_item,
android.R.id.text1,
items){
public View getView(int position, View convertView, ViewGroup parent) {
//User super class to create the View
View v = super.getView(position, convertView, parent);
TextView tv = (TextView)v.findViewById(android.R.id.text1);
//Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
//Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
tv.setCompoundDrawablePadding(dp5);
return v;
}
};
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case SHOW_DIALOG_SHARE:
return new AlertDialog.Builder(this)
.setIcon(R.drawable.icon)
.setTitle(R.string.app_name)
.setAdapter(mShareAdapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(CustomDialog.this, "Click on item " + item, Toast.LENGTH_SHORT).show();
}
})
.show();
}
return null;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
public void onBtnShareClick(View v) {
showDialog(SHOW_DIALOG_SHARE);
}
protected class ShareItem {
public final String text;
public final int icon;
public ShareItem(String text, Integer icon) {
this.text = text;
this.icon = icon;
}
@Override
public String toString() {
return text;
}
}
}
を共有カスタムを作成し、私の共有ダイアログが開かれます。すべての良い。
ここで、デバイスをポートレートモードにして、ボタンをクリックしてダイアログを開きます。その後、戻るを押して[共有ダイアログ]を閉じます。 デバイスを横向きモードに回転します。私はボタンをクリックしなかったが、突然共有ダイアログが再開された。
ネイティブの共有ダイアログを使用しようとすると、私はこのバグは表示されません。たぶんカスタム共有ダイアログが原因でしょうか?
ここで何が間違っているのか教えていただけますか?
これは機能します。すばらしいです。しかし、configChanges = "orientation"を追加する代わりに、それを修正する別の方法がありますか?私はここにconfigChangesを追加したくないからです。 – anticafe
私はやりたいと思っていません... – sivakg2000
私と一緒に働いていません –