Sending data back to the Main Activity in Android

后端 未结 12 1924
后悔当初
后悔当初 2020-11-22 01:36

I have two activities: main activity and child activity.
When I press a button in the main activity, the child activity is launched.

Now I want to send some dat

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:57

    Sending Data Back

    It helps me to see things in context. Here is a complete simple project for sending data back. Rather than providing the xml layout files, here is an image.

    Main Activity

    • Start the Second Activity with startActivityForResult, providing it an arbitrary result code.
    • Override onActivityResult. This is called when the Second Activity finishes. You can make sure that it is actually the Second Activity by checking the request code. (This is useful when you are starting multiple different activities from the same main activity.)
    • Extract the data you got from the return Intent. The data is extracted using a key-value pair.

    MainActivity.java

    public class MainActivity extends AppCompatActivity {
    
        private static final int SECOND_ACTIVITY_REQUEST_CODE = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
        }
    
        // "Go to Second Activity" button click
        public void onButtonClick(View view) {
    
            // Start the SecondActivity
            Intent intent = new Intent(this, SecondActivity.class);
            startActivityForResult(intent, SECOND_ACTIVITY_REQUEST_CODE);
        }
    
        // This method is called when the second activity finishes
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
    
            // Check that it is the SecondActivity with an OK result
            if (requestCode == SECOND_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {
    
                    // Get String data from Intent
                    String returnString = data.getStringExtra("keyName");
    
                    // Set text view with string
                    TextView textView = (TextView) findViewById(R.id.textView);
                    textView.setText(returnString);
                }
            }
        }
    }
    

    Second Activity

    • Put the data that you want to send back to the previous activity into an Intent. The data is stored in the Intent using a key-value pair.
    • Set the result to RESULT_OK and add the intent holding your data.
    • Call finish() to close the Second Activity.

    SecondActivity.java

    public class SecondActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_second);
        }
    
        // "Send text back" button click
        public void onButtonClick(View view) {
    
            // Get the text from the EditText
            EditText editText = (EditText) findViewById(R.id.editText);
            String stringToPassBack = editText.getText().toString();
    
            // Put the String to pass back into an Intent and close this activity
            Intent intent = new Intent();
            intent.putExtra("keyName", stringToPassBack);
            setResult(RESULT_OK, intent);
            finish();
        }
    }
    

    Other notes

    • If you are in a Fragment it won't know the meaning of RESULT_OK. Just use the full name: Activity.RESULT_OK.

    See also

    • Fuller answer that includes passing data forward
    • Naming Conventions for the Key String

提交回复
热议问题