Activity Data exchange

MainAcvity -> SubActivity
  • Implicit Intent refers to the creation of an Intent object that does not specify a specific recipient, but defines the actions, categories, and Data to be executed, and then lets the Android system find the Activity to be started according to the corresponding matching mechanism.

MainActivity:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Intent intent = new Intent(MainActivity.this,SubActivity.class);
                Intent intent = new Intent();
                intent.setAction("com.dengyu.ui.SubActivity");
                Bundle b = new Bundle();
                b.putString("name"."mask");
                b.putInt("age".23); intent.putExtras(b); startActivity(intent); }});Copy the code

Androidmanifest.xml:

<activity android:name=".SubActivity">
    <intent-filter>
        <action android:name="com.dengyu.ui.SubActivity" />
            
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
Copy the code

SubActivity:

tv = findViewById(R.id.tv);
Intent intent = getIntent();
Bundle extras = intent.getExtras();

String name = extras.getString("name");
int age =  extras.getInt("age");
ShowUtil.d("SubActivity",name + "," + age);
tv.setText(name + "," + age);
Copy the code
  • An explicit Intent is when an Intent object is created and the recipient (such as an Activity, Service, or BroadcastReceiver) is specified, because we already know the class name of the Activity or Service to be launched.

MainAcvtivity:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SubActivity.class);
                //Intent intent = new Intent();
                //intent.setAction("com.dengyu.ui.SubActivity");
                Bundle b = new Bundle();
                b.putString("name"."mask");
                b.putInt("age".23); intent.putExtras(b); startActivity(intent); }});Copy the code

Androidmanifest.xml:

<activity android:name=".SubActivity" />
Copy the code
MainAcvity -> SubActivity -> MainActivity

When the user completes the selection in the second Activity, the program automatically returns to the first Activity, which can fetch and display the results of the user’s selection in the second Activity.

This is done with intents and bundles. Instead of exchanging data between two activities, you use the startActivityForResult() method to start another Activity. After an Activity is started by calling the startActivityForResult() method, you can return the selected results to the original Activity when you close the newly started Activity.

/** * will start the Activity with the specified request code, and the program will get the result returned by the newly started Activity by overriding the onActivityResult() method@param intent
* @paramRequestCode specifies the requestCode used to start an Activity. It is used to identify */
public void startActivityForResult(@RequiresPermission Intent intent, int requestCode)
Copy the code

MianActivity:

public class MainActivity extends AppCompatActivity {
    public Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SubActivity.class);

                Bundle b = new Bundle();
                b.putString("name"."dnegyu");
                b.putInt("age".23);
                intent.putExtras(b);
// startActivity(intent);
                startActivityForResult(intent,0x01); }}); }@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01 && resultCode == Activity.RESULT_OK)
            Toast.makeText(MainActivity.this, data.getExtras().getString("key"), Toast.LENGTH_LONG).show(); }}Copy the code

SubActivity:

public class SubActivity extends AppCompatActivity {
    public TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sub);
        tv = findViewById(R.id.tv);
        final Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        String name = extras.getString("name");
        int age =  extras.getInt("age");
        ShowUtil.d("SubActivity",name + "," + age);
        tv.setText(name + "," + age);

        findViewById(R.id.sub_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(SubActivity.this,MainActivity.class);
                Bundle bb = new Bundle();
                bb.putString("key"."i am back"); intent.putExtras(bb); setResult(Activity.RESULT_OK,intent); finish(); }}); }}Copy the code

Activity Lifecycle

With the preceding list, we can clearly test the Activity declaration cycle.Copy the code

MainActivity:

public class MainActivity extends AppCompatActivity {
    public Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShowUtil.d("xixi"."MainActivity --> onCreate");
        setContentView(R.layout.activity_main);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,SubActivity.class);

                Bundle b = new Bundle();
                b.putString("name"."mask");
                b.putInt("age".23);
                intent.putExtras(b);
                startActivityForResult(intent,0x01); }}); }@Override
    protected void onStart(a) {
        super.onStart();
        ShowUtil.d("xixi"."MainActivity --> onStart");
    }

    @Override
    protected void onResume(a) {
        super.onResume();
        ShowUtil.d("xixi"."MainActivity --> onResume");
    }

    @Override
    protected void onPause(a) {
        super.onPause();
        ShowUtil.d("xixi"."MainActivity --> onPause");

    }

    @Override
    protected void onStop(a) {
        super.onStop();
        ShowUtil.d("xixi"."MainActivity --> onStop");
    }

    @Override
    protected void onDestroy(a) {
        super.onDestroy();
        ShowUtil.d("xixi"."MainActivity --> onDestroy");
    }
    
    @Override
    protected void onRestart(a) {
        super.onRestart();
        ShowUtil.d("xixi"."MainActivity --> onRestart");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x01 && resultCode == Activity.RESULT_OK)
            Toast.makeText(MainActivity.this, data.getExtras().getString("key"), Toast.LENGTH_LONG).show(); }}Copy the code

SubActivity:

public class SubActivity extends AppCompatActivity {
    public TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ShowUtil.d("xixi"."SubActivity --> onCreate");

        setContentView(R.layout.activity_sub);
        tv = findViewById(R.id.tv);
        final Intent intent = getIntent();
        Bundle extras = intent.getExtras();

        String name = extras.getString("name");
        int age =  extras.getInt("age");
        ShowUtil.d("SubActivity",name + "," + age);
        tv.setText(name + "," + age);

        findViewById(R.id.sub_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(SubActivity.this,MainActivity.class);
                Bundle bb = new Bundle();
                bb.putString("key"."i am back"); intent.putExtras(bb); setResult(Activity.RESULT_OK,intent); finish(); }}); }@Override
    protected void onStart(a) {
        super.onStart();
        ShowUtil.d("xixi"."SubActivity --> onStart");
    }

    @Override
    protected void onResume(a) {
        super.onResume();
        ShowUtil.d("xixi"."SubActivity --> onResume");
    }

    @Override
    protected void onPause(a) {
        super.onPause();
        ShowUtil.d("xixi"."SubActivity --> onPause");
    }

    @Override
    protected void onStop(a) {
        super.onStop();
        ShowUtil.d("xixi"."SubActivity --> onStop");
    }
    
    @Override
    protected void onRestart(a) {
        super.onRestart();
        ShowUtil.d("xixi"."SubActivity --> onRestart");
    }

    @Override
    protected void onDestroy(a) {
        super.onDestroy();
        ShowUtil.d("xixi"."SubActivity --> onDestroy"); }}Copy the code

From MainActivity to SubActivity, and then back to MainActivity. Observe logs.

  1. When loading MainActivity, the log is as follows:
D/xixi: MainActivity --> onCreate
D/xixi: MainActivity --> onStart
D/xixi: MainActivity --> onResume
Copy the code
  1. Click the Jump SubActivity button, and the log is as follows:
D/xixi: MainActivity --> onPause
D/xixi: SubActivity --> onCreate
D/xixi: SubActivity --> onStart
D/xixi: SubActivity --> onResume
D/xixi: MainActivity --> onStop
Copy the code
  1. In SubActivity, click the Back button to return to MainActivity. The log is as follows:
D/xixi: SubActivity --> onPause
D/xixi: MainActivity --> onRestart
D/xixi: MainActivity --> onStart
D/xixi: MainActivity --> onResume
D/xixi: SubActivity --> onStop
D/xixi: SubActivity --> onDestroy
Copy the code

Through observation, it is found that it is consistent with the official Activity lifecycle. As follows:

In the 1. Step, MainActivity is created \start\resume, and then the running state;

In the 2. Step, MainActivity jumps to the SubActivity, first into the paused state, while the activity is still visible, then SubActivity is placed on the top of the stack by creating \start\resume, MainActivity goes into the stop state, It’s not visible anymore.

In step 3, we return to MainActivity with a button, reactivating MainActivity to gain focus. The SubActivity executes finish and is destroyed.

The four states of an Activity

  • The current Activity, at the top of the Activity stack, is visible to the user and can be focused
  • An Activity that has lost focus while suspended is still visible, but cannot be killed by the system when memory is low.
  • Stop state This Activity is overwritten by other activities and is not visible, but it still holds all state and information. When memory is low, it will be killed by the system.
  • Destruction state The Activity ends, or the virtual machine process in which the Activity is running ends

A detailed description of four states and callback methods that can refer to the website: developer. The android, Google. Cn/guide/compo…