使用startActivityForResult()方法在Android活动之间传递数据并保留这些数据

时间:2017-09-20 09:51:56

标签: android android-intent startactivityforresult

我正在使用Android教程在两个活动之间传递数据 startActivityForResult()方法。这个例子很完美。但是,一旦我输入并传递主活动的值并再次尝试发送新值,则替换先前输入的值。我想要的是保持我输入的所有值并传递给主活动而不消失。请任何人都可以指导我。感谢您的帮助。

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical">

    <TextView
        android:id="@+id/textViewMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Message"
        android:textColor="#031241"
        android:textSize="20dp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:gravity="center_horizontal"
        android:onClick="getMessage"
        android:text="Get Message" />

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    TextView textViewMessage;

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

        // get The reference of The textView
        textViewMessage = (TextView) findViewById(R.id.textViewMessage);
    }

    // Method to handle the Click Event on GetMessage Button
    public void getMessage(View V) {
        // Create The  Intent and Start The Activity to get The message
        Intent intentGetMessage = new Intent(this, SecondActivity.class);
        startActivityForResult(intentGetMessage, 2);// Activity is started with requestCode 2
    }

    // Call Back method  to get the Message form other Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // check if the request code is same as what is passed  here it is 2
        if (requestCode == 2) {
            if (null != data) {
                // fetch the message String
                String message = data.getStringExtra("MESSAGE");
                // Set the message string in textView
                textViewMessage.setText("Message from second Activity: " + message);
            }
        }


    }
}

second.Xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editTextMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textColor="#000000"
        android:textSize="20sp"
        android:hint="Enter The Message" />

    <Button
        android:layout_marginTop="20dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Submit Message"
        android:onClick="submitMessage" />

</LinearLayout>

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

        EditText  editTextMessage;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
            // Get the reference of Edit Text
            editTextMessage=(EditText)findViewById(R.id.editTextMessage);
        }


        public void submitMessage(View V)
        {
            // get the Entered  message
            String message=editTextMessage.getText().toString();
            Intent intentMessage=new Intent();

            // put the message in Intent
            intentMessage.putExtra("MESSAGE",message);
            // Set The Result in Intent
            setResult(2,intentMessage);
            // finish The activity
            finish();
        }

}

1 个答案:

答案 0 :(得分:2)

使用此:

    String[] previousValue = new String[10]; // length of your array
    int i = 0;

// Call Back method  to get the Message form other Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // check if the request code is same as what is passed  here it is 2
        if (requestCode == 2) {
            if (null != data) {
                // fetch the message String
                String message = data.getStringExtra("MESSAGE");
                previousValue[i] = message;
                i++;
                // Set the message string in textView
                textViewMessage.setText("Message from second Activity: " + message);
            }
        }


    }

//然后你想要使用之前的值只是迭代数组。

for(int i = 0; previousValue.length(); i++){
//displaying all the previous values
Log.e("someTag","value : "+previousValue[i]);
}