Basic usage of startActivityForResult() in Android

Table of contents:

  1. The difference between startActivityForResult and startActivity

  2. The specific usage of startActivityForResult

2.1 Define the jump button

2.2 Set the activity to jump to

2.3 Writing of [second_layout.xml]

2.4 secondActivity

2.5 Get the returned result in mainActivity

1: The difference between startActivityForResult and startActivity

1、startActivity( )

Just jump to the target page. If you want to jump back to the current page, you must use startActivity( ) again. 

  1. startActivityForResult( ) 
         can complete this task at one time. When the program executes this code, if it jumps from T1Activity to the next Text2Activity, and when the Text2Activity calls the finish() method, the program will automatically jump Go back to T1Activity and call the onActivityResult() method in the previous T1Activity.

2: Specific usage of startActivityForResult

2.1: Define the jump button

main_layout.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >


    <Button
        android:id="@+id/choose_vehicle_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"

</RelativeLayout>

2.2 Set the activity to jump to

Note: You must first register the activity in AndroidManifest.xml

<activity
            android:
            android:screenOrientation="portrait" />

Add the following code to the function that initializes the page in MainActivity.java:

Bottom mChooseVehicle=findViewById(R.id.choose_vehicle_img);
mChooseVehicle.setOnClickListener( new View.OnClickListener() {
             @Override 
            public  void  onClick (View v)  {
            //MainActivity.this is the original Activity, secondActivity.class is the activity you want to jump to 
                Intent intent= new Intent(MainActivity.this ,secondActivity.class);
                startActivityForResult(intent, 7);

            }
        });

2.3 Writing second_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

< EditText 
    android:id = "@+id/text" 
    android:layout_width = "wrap_content" 
    android:layout_height = "wrap_content" 
    android:hint = "Please enter data"

    />
<Button 
    android:id="@+id/finish"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"

    />
</LinearLayout>

2.4 secondActivity

public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main1);
    final EditText text=(EditText) findViewById(R.id.text);
    Button finish=(Button) findViewById(R.id.finish);
    finish.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i=new Intent();
            i.putExtra("helloWorld", text.getText().toString());
            setResult(4,i);
            finish();
        }
    });
}
}

2.5 Get the returned result in mainActivity

private void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && resultCode == 4) {
            String s=data.getStringExtra("helloWorld");
            textview.setText(s);
        }
    }

Leave a Comment

Your email address will not be published. Required fields are marked *