开发者

I'm having trouble saving the state of this activity

开发者 https://www.devze.com 2023-03-16 12:06 出处:网络
public class Talk extends Activity { private ProgressDialog progDialog; int typeBar; TextView text1; EditText edit;
    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 onSavedInstanceState){

    super.onCreate(onSavedInstanceState);
    setContentView(R.layout.dorothydialog);
    text1 = (TextView)findViewById(R.id.dialog);
    edit = (EditText)findViewById(R.id.repsond);
    respond = (Button)findViewById(R.id.button01);

    if(onSavedInstanceState == null){


        text1.setText("Welcome! Enter your name!");
        respond.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                runDialog(2);
                name = edit.getText().toString();
                text1.setText("Cool! your name is "+name);

                respond.开发者_如何转开发setOnClickListener(new View.OnClickListener() {

                    @Override   
                    public void onClick(View v) {
                        runDialog(6);   




                    }
                });

            }
        });
        }else{
            reloadState();
        }

    }
    @Override
    public void onSaveInstanceState(Bundle outState){
        super.onSaveInstanceState(outState);

        super.onSaveInstanceState(outState);


    }
    public void reloadState(){

        text1.setText(textAtView);
    }
    @Override
    public void onDestroy(){
        super.onDestroy();

    }

I know some people may think this is simple, but im having trouble saving the state of this activity..if someone would help me out and provide an example based on the code i have above. It will be greatly appreciated!


You need to actually save the data in the Bundle. And then actually reload the data from the Bundle. You were very close.

@Override
public void onSaveInstanceState(Bundle outState){
    super.onSaveInstanceState(outState);
    outState.putString("text1", text1.getText().toString());
}
public void reloadState(){
    text1.setText(outState.getString("text1"));
}
0

精彩评论

暂无评论...
验证码 换一张
取 消