简体   繁体   中英

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 the 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM