开发者

How to restore when activity is destroyed or paused?

Okay so im 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. If anyone could look at it and tell me how i would save and restore is please it will be greatly appreciated.开发者_StackOverflow社区

Here is my code...

                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<Void, Integer, Void>(){

            @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;




    }

How can i save and restore when activity is destroyed?


You have to save your data before your activity is destroyed. You can test if it is going to be destroyed by using the isFinishing()

protected void onPause(){ if(isFinishing()){ saveData(); } }

then you neeed to reload your data onCreate()


You can try overiding

public Object onRetainNonConfigurationInstance() {

    //object returned here can always be recovered in getLaststNonConfigurationInstance()
    return something;
}

and use getLastNonConfigurationInstance() to get the state back.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜