开发者

How to save Activity State with this

I'm having a hard time saving the state of my activity so that when the activity is destroyed it can restore where th开发者_运维百科e 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.


Just override onSaveInstanceState(Bundle savedInstanceState)

and write the application state values you want to change to the Bundle parameter like this:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        // Save away the original text, so we still have it if the activity
        // needs to be killed while paused.
        outState.putString(ORIGINAL_CONTENT, mOriginalContent);
        outState.putInt("MyInt", 1);

    }

after that, you can retrieve in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the Bundle populated by this method will be passed to both)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜