【问题标题】:How can i save the desired data on EditText by using onSaveInstanceState() and onRestoreInstanceState() methods如何使用 onSaveInstanceState() 和 onRestoreInstanceState() 方法在 EditText 上保存所需的数据
【发布时间】:2017-04-18 10:45:40
【问题描述】:

我正在尝试设计一个 Android 应用程序,在该应用程序中,我已将数据从第一个活动发送到第二个活动。在第二个活动中,我正在使用此代码

 @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    }

当应用程序从横向模式切换到纵向模式时保存数据,但在我的应用程序中,即使我同时使用这两种模式,但数据没有被保存。轮换时,EditText View 中的应用程序正在销毁数据。

请检查下面的代码,给出我做错的地方来保存数据的建议
MainActivity.java

public class MainActivity extends AppCompatActivity {
    EditText name,age;
    TextView text_name,text_age;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(EditText) findViewById(R.id.name);
        age=(EditText) findViewById(R.id.age);
        text_name=(TextView) findViewById(R.id.name_edit);
        text_age=(TextView) findViewById(R.id.name_age);
        btn=(Button) findViewById(R.id.click);

        //Button Click to send data to another activity

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                String user_name=name.getText().toString();
                String user_age=age.getText().toString();
                bundle.putString("UName",user_name);
                bundle.putString("UAge",user_age);
                Intent intent=new Intent(MainActivity.this,SecondClass.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
    
}

SecondClass.java

public class SecondClass extends Activity {
    EditText name,age;
    TextView text_name,text_age;
    String namer,ager;
    private String savedName,savedAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity_layout);
        name=(EditText) findViewById(R.id.name);
        age=(EditText) findViewById(R.id.age);
        text_name=(TextView) findViewById(R.id.name_edit);
        text_age=(TextView) findViewById(R.id.name_age);

        if (savedInstanceState!=null)
        {
            savedInstanceState.get(savedName);
            name.setText(savedName);
            savedInstanceState.get(savedAge);
            age.setText(savedAge);
        }
        if (savedInstanceState==null)
        {
            Intent i=getIntent();
            Bundle bundle=i.getExtras();
            namer=bundle.getString("UName");
            name.setText(namer);
            ager=bundle.getString("UAge");
            age.setText(ager);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(savedName,namer);
        outState.putString(savedAge,ager);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState!=null)
        {
            savedInstanceState.get(savedName);
            name.setText(savedName);
            savedInstanceState.get(savedAge);
            age.setText(savedAge);
        }
        if (savedInstanceState==null)
        {
            Intent i=getIntent();
            Bundle bundle=i.getExtras();
            namer=bundle.getString("UName");
            name.setText(namer);
            ager=bundle.getString("UAge");
            age.setText(ager);
        }

    }
}

【问题讨论】:

  • 尝试在此声明outState.putString(savedAge,ager);之后移动super.onSaveInstanceState(outState);

标签: android android-activity sdk android-sdk-tools activity-lifecycle


【解决方案1】:

您使用键在保存的实例中放置和获取值,然后首先将数据放入 'outState' 然后调用'

super.onSaveInstanceState(outState);

试试这个:

public class SecondClass extends Activity {
    private static final String SAVED_NAME="savedName";
    private static final String SAVED_AGE="savedAge";
    EditText name,age;
    TextView text_name,text_age;
    String namer,ager;
    private String savedName,savedAge;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity_layout);
        name=(EditText) findViewById(R.id.name);
        age=(EditText) findViewById(R.id.age);
        text_name=(TextView) findViewById(R.id.name_edit);
        text_age=(TextView) findViewById(R.id.name_age);

        if (savedInstanceState!=null)
        {
            namer  = savedInstanceState.get(SAVED_NAME);
            name.setText(namer);
            ager = savedInstanceState.get(SAVED_AGE);
            age.setText(ager);
        }
        if (savedInstanceState==null)
        {
            Intent i=getIntent();
            Bundle bundle=i.getExtras();
            namer=bundle.getString("UName");
            name.setText(namer);
            ager=bundle.getString("UAge");
            age.setText(ager);
        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString(savedName,namer);
        outState.putString(savedAge,ager);
        super.onSaveInstanceState(outState);
        
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState!=null)
        {
            namer  = savedInstanceState.get(SAVED_NAME);
            name.setText(namer);
            ager = savedInstanceState.get(SAVED_AGE);
            age.setText(ager);
        }
        if (savedInstanceState==null)
        {
            Intent i=getIntent();
            Bundle bundle=i.getExtras();
            namer=bundle.getString("UName");
            name.setText(namer);
            ager=bundle.getString("UAge");
            age.setText(ager);
        }

    }
}

【讨论】:

  • @Hamideza ,感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-05
  • 1970-01-01
  • 1970-01-01
  • 2014-01-16
  • 1970-01-01
  • 2019-06-19
相关资源
最近更新 更多