Intent return data


Step one: Create a new project


We put the main activities of the project called A activities in preparation for a jump string startActivityForResult () in the A event.
    @Override
    protected void the onCreate (the Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        the setContentView (R.layout.activity_main);
        Final the Button B1 = (the Button) the findViewById (R.id.button1);
        b1.setOnClickListener (new new View.OnClickListener () {
            @Override
            public void the onClick (View V) {
                the Intent the Intent Intent new new = (MainActivity.this,
                        Main2Activity.class);
                intent.putExtra ( "AAAA", "People's Republic of China"); // pass a string
                startActivityForResult (intent, 1); // 1 second parameter is a jump custom request number.
            }
        });
    }
You look at it is no longer startActivity () jump, but the use of startActivityForResult (intent, 1); jump,
the startActivityForResult (Intent,. 1) the second parameter is the request code, the request code is yourself set it to be understood as a request number, this number is set their own.

Step two:


Another new activities, such as B activity, with the setResult B Activity () transferred back.
@Override
    protected void the onCreate (the Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        the setContentView (R.layout.activity_main2);
        Final the TextView TV1 = (the TextView) the findViewById (R.id.textView1);
        the Intent Intent its getIntent = ();
        String aaaa = intent.getStringExtra ( "aaaa") ; // pass over each other with aaaa, aaaa I used to pick up
        tv1.setText (aaaa);

        the Button B1 = Final (the Button) the findViewById (R.id.button1);
        b1.setOnClickListener (new new View.OnClickListener () {
            @Override
            public void the onClick (View V) {
                the Intent the Intent Intent new new = (); // here though . new one, but in fact it is just loaded with data of
                intent.putExtra ( "bbbb", "! do you like it what are you doing?"); // this is passed back
                setResult (RESULT_OK, intent); // RESULT_OK answer code.
                Finish ();
            }
        });
    }
the first parameter for an event processing result is returned upwardly, or generally except RESULT_OK RESULT_CANCLED these two values;
second parameter Intent with the past data transfer;
then call the finish () method to destroy the current activity.

third step:

A heavy-duty use in the event the onActivityResult function () to receive, rather than back to the onCreate () in
    @Override
    protected void the onActivityResult (requestCode int, int the resultCode, @Nullable the Intent Data) {
        super.onActivityResult (requestCode, the resultCode , Data);
        Switch (requestCode) {
            Case. 1:
                IF (the resultCode == RESULT_OK) {
                    String returnedData = data.getStringExtra ( "bbbb");
                    Toast.makeText (the this, returnedData, Toast.LENGTH_SHORT) the .Show ();
                }
                BREAK;
            default:
        }
    }
requestCode a request code, the request code and the request code a activities is the same.
resultCode answer code, the code is generally the answer is RESULT_OK.
data is passed over the other party in the data on the data, you have to get the other side to answer data, it is necessary to remove it from the data in.

At this point there is the effect of the operation.

the fourth step:

If you do not want to set up a dedicated return button, it can be resolved: the direct use of Android phones back button.

But this time, it should return the code written onBackPressed () in, because you press the Back button on your phone, it will execute this method. The method can be overloaded by this function.

    @Override
    public void onBackPressed() {
        Intent it=new Intent();
        it.putExtra("bbbb","新冠病毒在流行!");
        setResult(RESULT_OK,it);
        super.onBackPressed();
        return ;
    }

The following is additional information: The Back button will return data issues:

B activity value not pass over in A activity, it is what causes it?

Code is wrong, you should write as I did above, you should not write like this:

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent it=new Intent();
        it.putExtra("bbbb","新冠病毒在流行!");
        setResult(RESULT_OK,it);
        finish();
    }

Because this writing, it performs super.onBackPressed (), it returned to the BACK button called, it will first perform the finish (), resultCode code B activities such as RESULT_CANCELED has been set up, so there will be no A activity any return the data.


About Back key to execute onBackPressed () lack of data this problem, then got his information on the following website:
https://www.cnblogs.com/qiluboy/p/5308310.html

Its content is:
use the Andrews onBackPressed () method
explained one, onBackPressed () method
This method in
void android.app.Activity.onBackPressed ()
Andrews API it is explained:
public void onBackPressed ()
in API Level 5 Added
Called the when at The Activity has Detected at The Press of the User's the Back at The Key. Simply Finishes at The default Implementation at The Current Activity, the this the override But you CAN Whatever you want to do.

Understand is this: When you press this key, the calls this method.

This is the beginning of the extra Android 2.0, a new method for the Activity can be obtained by pressing the Back key event alone, the direct method can rewrite onBackPressed.

Second, the use onBackPressed () method

void onBackPressed public () {
        the Intent the Intent = new new the Intent ();
        intent.putExtra ( "data_return", "believe in yourself");
        setResult (RESULT_OK, the Intent);
        super.onBackPressed (); <br> return;
      }
attention: this method applies only to version 2.0 or later of sdk

On this return, I do not know how, I checked a lot of information, but said use return is not clear what role by return. I hope we can go to gather more information. I tried not return program also can run.

In addition, for everyone to talk about the difference between the back button and home button:

back key
Android program without having deliberately to quit when you press a key when the phone's back, the system will default to the calling program stack uppermost Activity of Destroy () method to destroy the current Activity. When this Activity has been started up in other Activity will recall OnCreate () method to create, after all Activity when the stack are the pop end, the application will come to an end. If the service exists such a program, it can also be in the right place at the listener process.

home button
hidden Android program, when you press the phone's Home button, the system will default to the calling program stack uppermost Activity of stop () method, and then the entire application will be hidden again when you click on the desktop phone when the application icon, the system will call OnResume the top of the Activity () method, this time not to re-open the program, but directly into, will directly show a stack uppermost Activity.

back bond default to the current Activity to finish, home button just to the Activity onStop.

Realization of hidden programs when you press the Home key effects:
1: Android needs to listen to key events prior to 2.0, a judge is not pressed the back button
2: Android 2.0 system then provides a onBackPressed () method, which is designed to monitor back key event, so just rewrite onBackPressed () method can be


void onBackPressed public () {
        // super.onBackPressed (); This sentence must log out, or went back to call the default handling of
        the Intent the Intent = new new the Intent ();
        intent.putExtra ( "data_return", "believe in yourself ");
        setResult (RESULT_OK, Intent);
        
      }

Published 72 original articles · won praise 79 · views 220 000 +

Guess you like

Origin blog.csdn.net/jintingbo/article/details/104642417