Sunday, July 31, 2011

How to persist the Dialog Box even after the Screen Orientation Change Android

I have read many articles about serious problem with dialog box that get disappear after screen orientation changes ! Actually the reason for the disappearance , when the screen orientation changes the Android sys. refresh the activity causing restart of activity class! But I have a solution for that hope u enjoy the matter!
Save state concept is used here, with android activity's override methods called
  1. Override the onRetainNonConfigurationInstance() method to return the Object you would like to retain.
  2. When your Activity is created again, call getLastNonConfigurationInstance() to recover your Object.
thats
public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}
This following lines should be declared in the on create method to get the state of the activity.  final MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data
= loadMyData();
}
I am posting the code Hope U understand more clear......
code having two layout file main.xml contain only one button  and second Layout file name contain two EDITTEXT and one button  http://developer.android.com/guide/topics/resources/runtime-changes.html      this is from where i got the info.... if u want to know more about this issue....  

package in.ultraneo.savestate;

import java.security.PublicKey;

import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SavestateinandroidActivity extends Activity {
/** Called when the activity is first created. */
Button btn1;
Button btn2;
EditText edit1,edit2;
String DIALOGSTATUS="0";
String restore = "";
CustomizeDialog custo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button)findViewById(R.id.button1);
MyDataObject data = (MyDataObject) getLastNonConfigurationInstance();
if (data == null) {
data = loadMyData();
new Toast(this).makeText(this, "there is no data",Toast.LENGTH_SHORT).show();
custo = new CustomizeDialog(this, R.layout.dialogstate);
DIALOGSTATUS="0";
edit1 = (EditText)custo.findViewById(R.id.editText1);
edit1.setText("");
edit2 = (EditText)custo.findViewById(R.id.editText2);
edit2.setText("");
btn2 = (Button)custo.findViewById(R.id.button1);
} else {
if(data.string3=="1")
{
DIALOGSTATUS="1";
restore = "1";
custo = new CustomizeDialog(this, R.layout.dialogstate);
edit1 = (EditText)custo.findViewById(R.id.editText1);
edit2 = (EditText)custo.findViewById(R.id.editText2);
edit1.setText(data.string1);
edit2.setText(data.string2);
custo.show();
}
}
custo.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
DIALOGSTATUS="1";
new Toast(getBaseContext()).makeText(getBaseContext(),"Show", 1000).show();
}
});
custo.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
DIALOGSTATUS="0";
edit1.setText("");
edit2.setText("");
restore ="0";
new Toast(getBaseContext()).makeText(getBaseContext(),"Dism", 1000).show();
}
});
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
restore = "1";
custo.show();
btn2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
custo.dismiss();
}
});
}
});
}
private MyDataObject loadMyData() {
MyDataObject da = new MyDataObject();
return da;
}

public Object onRetainNonConfigurationInstance() {
final MyDataObject data = collectMyLoadedData();
return data;
}

private MyDataObject collectMyLoadedData() {
if(restore.equals("1")){
MyDataObject da = new MyDataObject();
da.string1 = edit1.getText().toString();
da.string2 = edit2.getText().toString();
da.string3 = DIALOGSTATUS;
return da;
}else
{
return null;
}
}

}






********************
package in.ultraneo.savestate;

public class MyDataObject {
String string1;
String string2;
String string3;
public MyDataObject loadMyData()
{
return this;
}

}









No comments:

Post a Comment