如何保存状态时,它的活动与本被毁 [英] How to save state when activity it destroyed with this

查看:60
本文介绍了如何保存状态时,它的活动与本被毁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    public class Talk extends Activity {
private ProgressDialog progDialog;
int typeBar;
TextView text1;
EditText edit;
Button respond;
private String name;
private String textAtView;
private String savedName;

public void onCreate (Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    setContentView(R.layout.dorothydialog);


    text1 = (TextView)findViewById(R.id.dialog);
    edit = (EditText)findViewById(R.id.repsond);
    respond = (Button)findViewById(R.id.button01);

    respond.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            text1.setText("Welcome! Enter your name!");

            respond.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    name = edit.getText().toString();

                    text1.setText("Cool! your name is "+name);

                }
            });

        }
    });

}

}

好了,所以我想弄清楚如何我会保存此活动的状态。这只是从我的code小片段向您展示你们一个例子。所以,我要保存状态,这样当活动被破坏,用户会回来,他们不放过。
第二件事情,我想告诉只需5第二次进度对话框每按一下按钮之间的微调。

Okay so i want to figure out how i would save the state of this activity. this is just a small snippet from my code to show you guys an example. So i want to save the state so when the activity is destroyed the user will come back where they left off.
Second thing, I would like to show a quick 5 second Progress dialog spinner between each button click.

推荐答案

对于第二件事

这应该工作:

public class TestActivity extends Activity implements Runnable, OnClickListener {
private TextView tv;
private ProgressDialog pd;
private Button btn;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    tv = (TextView) this.findViewById(R.id.tv);     
    btn = (Button)findViewById(R.id.btn);

    tv.setText("initial text");

    btn.setOnClickListener(this);
}

public void onClick(View v) {
    pd = ProgressDialog.show(TestActivity.this, "Please wait...", "Details here", true, false);

    Thread thread = new Thread(TestActivity.this);
    thread.start();
}
public void run() {
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    handler.sendEmptyMessage(0);
}

private Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        pd.dismiss();
        tv.setText("text after 5 sec passed");
    }
};
}

这篇关于如何保存状态时,它的活动与本被毁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆