Android 응용 프로그램을 디자인하려고합니다. 해당 응용 프로그램에서 First Activity에서 Second Activity로 데이터를 보냈습니다. 데이터가 아닌하지만 두 번째 활동에서 내 응용 프로그램 세로 모드에 있지만 가로로 응용 프로그램을 이동도 나는이 두 가지를 사용하고 데이터를 저장하기 위해이 코드onSaveInstanceState() 및 onRestoreInstanceState() 메서드를 사용하여 EditText에 원하는 데이터를 저장하는 방법
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
을 사용하고 있습니다 구원 받았다. 회전시 데이터가 파괴되어 어떤 응용 프로그램이 EditText
보기에있는 것입니다.
public class MainActivity extends AppCompatActivity {
EditText name,age;
TextView text_name,text_age;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(EditText) findViewById(R.id.name);
age=(EditText) findViewById(R.id.age);
text_name=(TextView) findViewById(R.id.name_edit);
text_age=(TextView) findViewById(R.id.name_age);
btn=(Button) findViewById(R.id.click);
//Button Click to send data to another activity
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle bundle=new Bundle();
String user_name=name.getText().toString();
String user_age=age.getText().toString();
bundle.putString("UName",user_name);
bundle.putString("UAge",user_age);
Intent intent=new Intent(MainActivity.this,SecondClass.class);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
데이터
을 저장하는 실수를하고있는 중이 제안을주십시오
public class SecondClass extends Activity {
EditText name,age;
TextView text_name,text_age;
String namer,ager;
private String savedName,savedAge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity_layout);
name=(EditText) findViewById(R.id.name);
age=(EditText) findViewById(R.id.age);
text_name=(TextView) findViewById(R.id.name_edit);
text_age=(TextView) findViewById(R.id.name_age);
if (savedInstanceState!=null)
{
savedInstanceState.get(savedName);
name.setText(savedName);
savedInstanceState.get(savedAge);
age.setText(savedAge);
}
if (savedInstanceState==null)
{
Intent i=getIntent();
Bundle bundle=i.getExtras();
namer=bundle.getString("UName");
name.setText(namer);
ager=bundle.getString("UAge");
age.setText(ager);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(savedName,namer);
outState.putString(savedAge,ager);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState!=null)
{
savedInstanceState.get(savedName);
name.setText(savedName);
savedInstanceState.get(savedAge);
age.setText(savedAge);
}
if (savedInstanceState==null)
{
Intent i=getIntent();
Bundle bundle=i.getExtras();
namer=bundle.getString("UName");
name.setText(namer);
ager=bundle.getString("UAge");
age.setText(ager);
}
}
}
'super.onSaveInstanceState (outState)를 이동하려고;'이 문 후'outState.putString (savedAge, 어른),' –