Talk Android together (241st time: data transfer between activities in Android 3)

Hello, ladies and gentlemen. Last time we talked about the example of data transfer between activities in Android. This time we will continue to talk about this example. Don't talk about it. Turn it around. Let's Talk Android!

The audience, this time can be seen as the end result of jump and data transfer between activities. We sorted out all the processes, and then demonstrated them with complete code. Here are the specific codes for your reference:

package com.example.talk8.blogappall;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

//Introduce the interaction and data transfer between activities and fragments
public class ActivityA extends AppCompatActivity {
    private Button mBtStartActB;
    private Button mBtStartActBForData;
    private Intent mIntent = null;
    private int REQUEST_CODE = 1;
    public static final String KEY = "value_key";
    private static final String TAG = "DataStorage";

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

        mBtStartActB =(Button)findViewById(R.id.start_activity_b);
        mBtStartActBForData =(Button)findViewById(R.id.start_activity_b_for_data);

        mBtStartActB.setOnClickListener(v -> startActivityB());
        mBtStartActBForData.setOnClickListener(v->startActivityBForData());
    }

    private void startActivityB() {
        int value = 5;
        if(mIntent != null)
            mIntent = null;

        //send Data to ActivityB
        mIntent = new Intent(ActivityA.this,ActivityB.class);
        mIntent.putExtra(KEY,value);
        startActivity(mIntent);
        Log.i(TAG, "startActivityB and send Data: "+value);
    }
    //want to get data from ActivityB
    private void startActivityBForData() {
        if(mIntent != null)
            mIntent = null;

        mIntent = new Intent(ActivityA.this,ActivityB.class);
        startActivityForResult(mIntent,REQUEST_CODE);
        Log.i(TAG, "startActivityB For getting Data: ");
    }

    //get data from ActivityB
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        //Note: confirm data source
        if(requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
           int result = data.getExtras().getInt(KEY);
            Log.i(TAG, "onActivityResult: ActivityA get data from ActivityB : "+result);
        }else {
            Log.i(TAG, "onActivityResult: it is not a effective data");
        }
    }
}
package com.example.talk8.blogappall;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

public class ActivityB extends AppCompatActivity {
    private static final String TAG = "DataStorage";
    private Intent mIntent;
    private int value;

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

        //get data from ActivityA
        mIntent = getIntent();

        if(mIntent != null ) {
            if (mIntent.hasExtra(ActivityA.KEY)) {
                //get data from ActivityA
                value = mIntent.getIntExtra(ActivityA.KEY,0);
                if(value != 0) {
                    Log.i(TAG, "onCreate: ActivityB get Data from ActivityA: "+value);
                }else {
                    Log.i(TAG, "onCreate: ActivityB get Data is null");
                }
            } else {
                //send data to ActivityA
                value = 9;
                Log.i(TAG, "onCreate: ActivityB send Data: "+value+" to ActivityA ");
                mIntent = null;
                mIntent = new Intent();
                mIntent.putExtra(ActivityA.KEY,value);
                setResult(RESULT_OK,mIntent);
            }
        }
    }
}

In the code, we mainly optimized and added log s and comments. The following is the running result of the program, please refer to:

 //Click the button to jump from A to B, and send data 5 to B
 I/DataStorage: startActivityB and send Data: 5
 //B received the data from A 5
 I/DataStorage: onCreate: ActivityB get Data from ActivityA: 5
 //Click the button to jump from A to B, and request B to send data to A
 I/DataStorage: startActivityB For getting Data:
 //After jump to B, B sends data 9 to A
 I/DataStorage: onCreate: ActivityB send Data: 9 to ActivityA
 //A received the data from B 9
 I/DataStorage: ActivityA get data from ActivityB : 9

Ladies and gentlemen, let's introduce the example of data transfer between activities in Android here. If you want to know what's more, let's listen to the next step!

Tags: Android

Posted on Mon, 01 Jun 2020 08:17:25 -0700 by anthonyaykut