【问题标题】:How to save state when activity it destroyed with this如何在活动被破坏时保存状态
【发布时间】:2011-06-30 05:13:21
【问题描述】:
    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);

                }
            });

        }
    });

}

}

好的,所以我想弄清楚如何保存此活动的状态。这只是我的代码中的一个小sn-p,向你们展示了一个例子。所以我想保存状态,这样当活动被破坏时,用户会回到他们离开的地方。
第二件事,我想在每个按钮单击之间显示一个快速的 5 秒进度对话框微调器。

【问题讨论】:

  • This link 解释了应用程序/活动生命周期及其回调方法,this link 解释了保存持久状态。第二件事,你真的想要在每个按钮点击之间有一个 5 秒的进度对话框微调器吗?还是您希望有 5 秒的时间暂停用户输入?

标签: android


【解决方案1】:

第二件事

这应该可行:

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");
    }
};
}

【讨论】:

  • 现在唯一的事情是我希望 runDialog() 在上面的方法中的 textView 上显示文本之前显示。
  • 所以只需调用 runDialog();之前 textView.setText();
  • 我做到了,但它仍然一起做。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-02-26
  • 1970-01-01
  • 1970-01-01
  • 2016-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多