ダイアログについて話しているのですか?そのために新しいアクティビティを作成する必要はありません。カスタム ダイアログを作成し、引数を onDialogPositiveClick() リスナーで返します。
まず、MainActivity は TablesDialogFragment.NoticeDialogListener を実装する必要があります。
次に、ダイアログ クラスで次のようにします。
public class TablesDialogFragment extends DialogFragment implements OnLongClickListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ArrayList<String> mSelectedItems = new ArrayList<String>();
String title = getArguments().getString("title");
final String[] tables = getArguments().getStringArray("key");
ContextThemeWrapper ctw = new ContextThemeWrapper( getActivity(), R.style.AlertDialogCustom);
AlertDialog.Builder builder = new AlertDialog.Builder(ctw);
if(tables.length !=0){
builder.setTitle(title)
.setMultiChoiceItems(tables, null,
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(tables[which]);
} else if (!isChecked) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(tables[which]);
}
}
})
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// User clicked OK, so save the mSelectedItems results somewhere
// or return them to the component that opened the dialog
String[] selected = mSelectedItems.toArray(new String[mSelectedItems.size()]);
//Here I pass the String[] to the MainActivity
mListener.onDialogPositiveClick(TablesDialogFragment.this, selected);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Collapse SearchView
MainActivity.getSearchMenuItem().collapseActionView();
}
});
} else{
builder.setTitle("Sorry to tell you this, but... :(");
builder.setMessage("Why don't you try something different?")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Collapse SearchView
MainActivity.getSearchMenuItem().collapseActionView();
}
});
}
return builder.create();
}
次に、MainActivity で。
private String[] selectedItems;
public void callDialog(){
TablesDialogFragment newFragment = new TablesDialogFragment();
Bundle args = new Bundle();
String[] array = {"hey", "you"};
args.putString("title", "Title of The Dialog");
args.putStringArray("key", array);
newFragment.setArguments(args);
newFragment.show(getSupportFragmentManager(), "Table");
}
@Override
public void onDialogPositiveClick(DialogFragment dialog, String[] selected) {
// TODO Auto-generated method stub
selectedItems = selected;
//Do Whatever you want with the arguments in your MainActivity
}