Android的:如何保存数据(意向的数据) [英] Android : How to save data (Intent's Data)

查看:105
本文介绍了Android的:如何保存数据(意向的数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个例子code,通过使用三个活动codeS下面意图传递的数据。

There is an example code that passed data by using Intent below the three Activity codes.

活动A

public class A extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_A);
}

public void onButton1Clicked(View v) {
    Intent intent = new Intent(getApplicationContext(), B.class);
    startActivity(intent);
}

活动B

public class B extends AppCompatActivity {

TextView tv;
ImageButton ib;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_B);

    tv = (TextView) findViewById(R.id.textView);
    ib = (ImageButton) findViewById(R.id.imageButton);
    ib.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(B.this, C.class);
            startActivityForResult(intent, 2);
        }
    });

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 2) {
        if (data != null) {
            String message = data.getStringExtra("DATA");
            tv.setText(message);
        }
    }
}

public void onButton2Clicked(View v) {
    Intent intent = new Intent(getApplicationContext(), C.class);
    startActivity(intent);
}

活动ç

public class C extends AppCompatActivity {

EditText et;
Button btn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_C);

    et = (EditText) findViewById(R.id.editText);
    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String data = et.getText().toString();
            Intent intent = new Intent();
            intent.putExtra("DATA", data);
            setResult(2, intent);
            finish();
        }
    });
}

活动的顺序是A - B - ç

The order of activity is A - B - C.

在A,有一个可以移动到B一个按钮,B中有一个按钮(可以移动到C)和TextView中。在C语言中,一个按钮(可以移动再次b)和EditText上。

In A, there is a Button that can move to B, and in B there are a Button(can move to C) and TextView. In C, a Button(can move to B again) and EditText.

例如,使用startActivityForResult我有输出的TextView(B)接受进入到B时,这是由输入的EditText在C.但移动B插入A(返回),TextView中的插入被消失后的文本再次。此外,即使在进入成C,不存在的EditText的插入,也

For example, Using 'startActivityForResult' I have output as TextView(B) that received a text which was input by EditText in C. But after moving B into A(Back), an insertion of TextView gets disappeared when entering to B again. In addition, even if entering into C, there is no insertion of EditText, too.

我真的需要通过输入如的EditText知道'保存code'到变量时$ P $用C pssing一个按钮。

I would really need and know 'Save code' into variable by inputting as EditText when pressing a button in C.

在这种情况下,我怎么能加上code'的保持插入值或者插入值仍然是戒烟收到类似数据的应用程序或移动到活动?

In this case, HOW can I add ‘Code’ the remain the insertion value either the insertion value remains by quitting the application or moves to Activity that received DATA like A?

谢谢您的合作与关注。

推荐答案

要保持你的活动的状态,你必须重写的onSaveInstanceState 方法。存储你TextViews的在此方法的值和的EditText。例如,让我们来谈谈活动B.在活动B,你会做这样的事情:

To maintain the state of your activity, you have to override onSaveInstanceState method. Store the value of your TextViews and EditText in this method. For example, let's talk about Activity B. In activity B you would do something like this:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the user's current game state
    String text = tv.getText().toString();
    savedInstanceState.putString("mytext", text);

    // Always call the superclass so it can save the view hierarchy state
    super.onSaveInstanceState(savedInstanceState);
}

然后在你的的onCreate 做到这一点:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); // Always call the superclass first
   // Set Content View and initialize the views
    // Check whether we're recreating a previously destroyed instance
    if (savedInstanceState != null) {
        // Restore value of members from saved state
        String mytext = savedInstanceState.getString("mytext");
        tv.setText(mytext);
    } else {
        // Probably initialize members with default values for a new instance
    }
    ...
}

您可以阅读更多有关重新创建一个活动。

这篇关于Android的:如何保存数据(意向的数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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