在Android中保存活动移至另一个活动时 [英] Save a Activity in Android When moved to another Activity

查看:99
本文介绍了在Android中保存活动移至另一个活动时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
如何保存Android应用程序的状态?

我当前正在处理具有以下行为的应用程序:我正在使用2 Edittext,当我按下按钮时输入了一些值它会转到下一页或活动,但是问题出在这里,当我返回活动时,我在edittext中找不到值.请帮助我..请让我知道代码...在此先感谢....

I'm currently working an application that has the following behavior : i am using 2 Edittext and there are some value which i type in it and when i press the button it will move on to the next page or activity ,But the problem is here when i return back to activity i dont find the values in edittext PLease help me .. Please let me know the code ... Thanks in Advance....

这是我的代码....

public class TestsampleActivity extends Activity 
{
        Button b1,b2;
        TextView t1;
        EditText e1;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            b1= (Button)findViewById(R.id.btn1);
            b2= (Button)findViewById(R.id.btn2);
            t1=(TextView)findViewById(R.id.tv1);
            e1=(EditText)findViewById(R.id.et1);


            b1.setOnClickListener(new Button.OnClickListener() 
            {   
                 public void onClick (View v){ add(); }
            });

            b2.setOnClickListener(new Button.OnClickListener() 
            { 
                 public void onClick (View v){ del(); }
            });
       }
       public void add()
       {
            String s1= e1.getText().toString();
            Intent intent=new Intent(this,next.class);
            intent.putExtra("name",s1);
            startActivity(intent);


            /* String s1= e1.getText().toString();
            //t1.append(s1);
            t1.setText(s1);
            */
       }
       public void del()
       {             
           e1.setText("");
           t1.setText("");
       }
}

推荐答案

在加载新活动之前,您必须重写onSaveInstanceState(Bundle b)来保存数据.

You must override the onSaveInstanceState(Bundle b) to save your data before loading the new activity.

此外,您还必须还原覆盖onRestoreInstanceState并将其取回的值...

Also you must restore overriding the onRestoreInstanceState and getting the values back...

您可以在此处获取更多信息:

you can se more information here:

使用保存实例状态"保存Android活动状态

http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)

这篇关于在Android中保存活动移至另一个活动时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆