当 Activity 被破坏或暂停时如何恢复?

发布于 2024-11-16 20:54:36 字数 2465 浏览 5 评论 0原文

好吧,我很难保存活动的状态,以便当活动被破坏时它可以恢复用户上次离开的位置。这是我的源代码。如果有人能看一下它并告诉我如何保存和恢复,我将不胜感激。

这是我的代码...

                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;

} 返回 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<Void, Integer, Void>(){

            @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;




    }

Activity 被销毁时如何保存和恢复?

Okay so im 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. If anyone could look at it and tell me how i would save and restore is please it will be greatly appreciated.

Here is my code...

                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<Void, Integer, Void>(){

            @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;




    }

How can i save and restore when activity is destroyed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

超可爱的懒熊 2024-11-23 20:54:36

您必须在您的活动被销毁之前保存您的数据。 来测试它是否会被销毁

您可以使用 isFinishing() protected void onPause(){
如果(正在完成()){
保存数据();
}
那么

你需要在Create()上重新加载数据

You have to save your data before your activity is destroyed. You can test if it is going to be destroyed by using the isFinishing()

protected void onPause(){
if(isFinishing()){
saveData();
}
}

then you neeed to reload your data onCreate()

小姐丶请自重 2024-11-23 20:54:36

您可以尝试覆盖

public Object onRetainNonConfigurationInstance() {

    //object returned here can always be recovered in getLaststNonConfigurationInstance()
    return something;
}

并使用 getLastNonConfigurationInstance() 来恢复状态。

You can try overiding

public Object onRetainNonConfigurationInstance() {

    //object returned here can always be recovered in getLaststNonConfigurationInstance()
    return something;
}

and use getLastNonConfigurationInstance() to get the state back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文