Wednesday, 7 August 2013

Resume Alert Dialog state on Back button pressed

Resume Alert Dialog state on Back button pressed

I have to deal with Alert Dialog in my android app. In my first activity
there is one alert dialog.On particular action, alert dialog appears. When
user clicks yes button of Alert Dialog, user moves to second activity. In
my second activity, I have one button. On pressing of this button Alert
Dialog should be prompted again with same state. Is it possible to resume
the state of Alert Dialog on Back button pressed? code for dialog...
// Alert Dialog for input
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog dialogDetails = null;
switch (id) {
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder dialogbuilder = new AlertDialog.Builder(this);
dialogbuilder.setView(dialogview);
dialogDetails = dialogbuilder.create();
break;
case LIST_DIALOG :
LayoutInflater inflater2 = LayoutInflater.from(this);
View dialogview1 = inflater2.inflate(R.layout.listdialog, null);
AlertDialog.Builder dialogbuilder2 = new AlertDialog.Builder(this);
dialogbuilder2.setView(dialogview1);
dialogDetails = dialogbuilder2.create();
}
return dialogDetails;
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case DIALOG_LOGIN:
final AlertDialog alertDialog = (AlertDialog) dialog;
Button btnSave = (Button) alertDialog.findViewById(R.id.btnSave);
Button btnCancel = (Button) alertDialog.findViewById(R.id.btnCancel);
etList = (EditText) alertDialog.findViewById(R.id.etList);
btnSave.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
newList = etList.length();
// TODO Auto-generated method stub
if ( newList <= 0)
{
etList.requestFocus();
Toast.makeText(getBaseContext(), "Enter Name of List",
1000).show();
}
else
{
listData =
etList.getText().toString().replaceAll(System.getProperty("line.separator"),
"");
if(listData.length() > 0)
{
dataPass(listData);
createfile(listData);
}
else
{
Toast.makeText(getBaseContext(), "Enter Name of
List", 1000).show();
}
}
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(DIALOG_LOGIN);
etList.setText("");
}
});
break;
case LIST_DIALOG:
AlertDialog alertDialog1 = (AlertDialog) dialog;
lvDialog = (ListView) alertDialog1.findViewById(R.id.list1);
TextView tvAdd = (TextView) alertDialog1.findViewById(R.id.txtAdd);
tvDone = (TextView) alertDialog1.findViewById(R.id.txtDone);
if(listName.length() <= 7)
{
tvAdd.setText("Add Friends to" +" " + listName );
}
else
{
tvAdd.setText("Add Friends to" +" " + listName.substring(0, 5)
+ "..");
}
tvDone.setOnClickListener(new OnClickListener() {
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if( colorRow > 0 )
{
AlertDialog builder = new
AlertDialog.Builder(NewFriendList.this).create();
builder.setTitle("Save to List");
builder.setMessage("Do You Want to Save?");
builder.setButton("Save", new
DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int
which)
{
removables = new ArrayList<String>();
for(int c= 0; c<checked.size(); c++)
{
int key = checked.keyAt(c);
if(checked.get(key, false))
{
removables.add(arr2.get(key));
meMap.put(listName, arr2.get(key));
}
}
try
{
writeToFile(removables , listName);
dismissDialog(LIST_DIALOG);
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
builder.setButton2("Cancel", new
DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int
which)
{
chkText.setTextColor(Color.BLACK);
colorRow = 0;
}
});
builder.show();
}
else
Toast.makeText(getBaseContext(), "Select Friends",
1000).show();
}
});
lvDialog.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
checked = lvDialog.getCheckedItemPositions();
chkText = (CheckedTextView) arg1.findViewById(R.id.txt);
if(checked.get(arg2))
{
chkText.setTextColor(Color.CYAN);
colorRow ++;
}
else
{
chkText.setTextColor(Color.BLACK);
//colorRow--;
colorRow = 0;
}
}
});
// Cancel Alert Dialog
ImageView ivCancel = (ImageView)
alertDialog1.findViewById(R.id.imgCancel);
ivCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
dismissDialog(LIST_DIALOG);
arr2.clear();
}
});
// Friend List
showFriendList();
break;
}

No comments:

Post a Comment