如何用这个保存活动状态

发布于 2024-11-16 06:51:05 字数 2983 浏览 3 评论 0原文

我很难保存活动的状态,以便当活动被破坏时它可以恢复用户上次停止的位置。这是我的源代码。我如何保存和恢复它。


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

现在我不知道从哪里开始。感谢谁能提供帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吲‖鸣 2024-11-23 06:51:05

只需覆盖 onSaveInstanceState(Bundle savingInstanceState)

并将要更改的应用程序状态值写入 Bundle 参数,如下所示:

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

    }

之后,您可以在 onCreate(Bundle) 或 onRestoreInstanceState(Bundle) 中检索(Bundle 填充为该方法将传递给两者)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文