如何用这个保存活动状态
我很难保存活动的状态,以便当活动被破坏时它可以恢复用户上次停止的位置。这是我的源代码。我如何保存和恢复它。
public class DorothyTalk extends Activity{
Handler handler = new Handler();
int typeBar;
TextView text1;
EditText edit;
Button respond;
private String name;
private ProgressDialog progDialog;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dorothydialog);
text1 = (TextView)findViewById(com.fttech.da.R.id.dialog);
edit = (EditText)findViewById(com.fttech.da.R.id.repsond);
respond = (Button)findViewById(com.fttech.da.R.id.button01);
Talk();
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0: // Spinner
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Loading...");
progDialog.setProgress(100);
return progDialog;
}
return progDialog;
}
public void Talk(){
text1.setText("Welcome what is your name?");
respond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = edit.getText().toString();
new AsyncTask(){
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
text1.setText("Nice to meet you "+name);
dismissDialog(typeBar);
}
@Override
protected void onPreExecute() {
typeBar = 0;
showDialog(typeBar);
}
}.execute((Void)null);
}
});
}
public void onBackPressed(){
int i = Log.d("CDA", "onBackPressed Called");
Context localContext = getApplicationContext();
Intent localIntent = new Intent(localContext, mainMenu.class);
startActivityForResult(localIntent, 0);
return;
}
}
现在我不知道从哪里开始。感谢谁能提供帮助。
I'm having a hard time saving the state of my activity so that when the activity is destroyed it can restore where the user last left off. Here is my source code. How do I save and restore it.
public class DorothyTalk extends Activity{
Handler handler = new Handler();
int typeBar;
TextView text1;
EditText edit;
Button respond;
private String name;
private ProgressDialog progDialog;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dorothydialog);
text1 = (TextView)findViewById(com.fttech.da.R.id.dialog);
edit = (EditText)findViewById(com.fttech.da.R.id.repsond);
respond = (Button)findViewById(com.fttech.da.R.id.button01);
Talk();
}
protected Dialog onCreateDialog(int id) {
switch(id) {
case 0: // Spinner
progDialog = new ProgressDialog(this);
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setMessage("Loading...");
progDialog.setProgress(100);
return progDialog;
}
return progDialog;
}
public void Talk(){
text1.setText("Welcome what is your name?");
respond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = edit.getText().toString();
new AsyncTask(){
@Override
protected Void doInBackground(Void... arg0) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
text1.setText("Nice to meet you "+name);
dismissDialog(typeBar);
}
@Override
protected void onPreExecute() {
typeBar = 0;
showDialog(typeBar);
}
}.execute((Void)null);
}
});
}
public void onBackPressed(){
int i = Log.d("CDA", "onBackPressed Called");
Context localContext = getApplicationContext();
Intent localIntent = new Intent(localContext, mainMenu.class);
startActivityForResult(localIntent, 0);
return;
}
}
Right now I do not know where to begin. Thanks to who ever can help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需覆盖 onSaveInstanceState(Bundle savingInstanceState)
并将要更改的应用程序状态值写入 Bundle 参数,如下所示:
之后,您可以在 onCreate(Bundle) 或 onRestoreInstanceState(Bundle) 中检索(Bundle 填充为该方法将传递给两者)
Just override onSaveInstanceState(Bundle savedInstanceState)
and write the application state values you want to change to the Bundle parameter like this:
after that, you can retrieve in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both)