Use intent to return data to explain the previous activity instance (use onBackPressed() to return data to solve the previous activity problem)

1. Add controls to the layout of FirstActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.firstactivitya.FirstActivity">

    <Button
        android:id="@+id/goto_second_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="to SecondActivity"/>

</LinearLayout>
2. Modify the code in FirstActivity:
public class FirstActivity extends AppCompatActivity {

    private Button gotoSecondBtn;
    private static  final String TAG = "FirstActivity";


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

        gotoSecondBtn = findViewById(R.id.goto_second_btn);
        //Monitor buttons
        gotoSecondBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Jump with intent
                Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                //Start jump. Here, use the startActivityForResult() method, which will return a data to the previous activity when the jump activity page is destroyed
                /**
                 * param1:intent
                 * param2: Request code, used to determine the source of data during callback
                 */
                startActivityForResult(intent, 1);
            }
        });
    }
}
3. Send message in SecondActivity:
public class SecondActivity extends AppCompatActivity {

    private Button secondBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        secondBtn = findViewById(R.id.second_btn);
        secondBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Create Intent object
                Intent intent = new Intent();
                //Assign by put () as a key value pair
                intent.putExtra("hi", "hi FirstActivity");
                //Return the result param1 to the previous activity through setResult(): processing result code
                setResult(1, intent);
                finish();
            }
        });
    }
    }
4. At last, copy onActivityResult() in FirstActivity to receive the message:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e(TAG, "onActivityResult1: "+data );
        switch (resultCode) {
            //The following 1 is the 1 in startActivityForResult(intent, 1); the
            case 1:
                //1 here is setResult(1, intent); 1 in
                if (resultCode==1){
                    String str = data.getStringExtra("hello");
                    Log.e(TAG, "onActivityResult2: "+str );
                    String str2 = data.getStringExtra("hi");
                    Log.e(TAG, "onActivityResult3: "+str2 );
                }
                break;


            default:
                break;

        }
    }
}
OK, so you can transfer values, but you may have to ask again, String str = data.getStringExtra("hello"); where is it from? This is the code I want to write below. Add it in SecondActivity:
    @Override
    public void onBackPressed() {
//        super.onBackPressed();
        Intent intent = new Intent();
        intent.putExtra("hello","Hello FirstActivity");
        setResult(1,intent);
        finish();
    }
You can click the return button instead of clicking the button to return the data. Look at my code carefully. You can see that I will / / super.onBackPressed(); It's commented out, because if it's not commented, it will call the default processing method of onBackPressed(), and it can't return data to the previous activity. This is my mistake. Of course, I can use the previous method to monitor the return button:
 @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent();
            intent.putExtra("hello", "Hello FirstActivity");
            setResult(1, intent);
            finish();
        }
        return super.onKeyDown(keyCode, event);

    }

Finally, you can see the operation effect:

Tags: Android xml encoding

Posted on Fri, 27 Mar 2020 10:19:41 -0700 by rockyracoon