Summary of learning experience based on android startActivityForResult

Source: Internet
Author: User

From last night till now, I finally debugged A startActivityForResult example. Some of my online experiences were too complicated or vague.
A main interface (main Activity) can be connected to many different sub-function modules (Child Activity). After the sub-module completes the tasks, it will return to the main interface, at the same time, some sub-modules are returned to the main Activity for processing. Using startActivity to start the main interface is a new Intent instance, and the accessed main interface is still not called below the activity stack. The biggest problem in this way is that, when the interface is not returned, multiple sub-functional modules cannot work together to provide data or services to the main interface. StartActivityForResult will be used at this time!
Purpose: MainActivity. java is the main interface, SecondActivity. java is a sub-function module. To start second from main, after receiving the data sent from main, second will report the result to main by pressing the OK button, and closes and returns the result to main.
Specific implementation:
Divided into four parts:
1. Set the sendBuddle button in MainActivity to send data to SecondActivity and go to the second interface. Key listening code:
Copy codeThe Code is as follows: class sendButtonListen implements OnClickListener {
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent intent = new Intent ();
String str = "dajia hao ";
Intent. putExtra ("send", str );
Intent. setClass (MainActivity. this, SecondActivity. class );
StartActivityForResult (intent, 0 );
}
}

2. The OnCreate function in SecondActivity receives data from intent in main.
Copy codeThe Code is as follows: @ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_second );
OkButton = (Button) findViewById (R. id. OK); // press this OK Button to return to main.
Intent intent = getIntent ();
String getStr = intent. getStringExtra ("send ");
TextView TV = (TextView) findViewById (R. id. sendText );
TV. setText (getStr );
Toast. makeText (SecondActivity. this,
"The data passed back from MainActivity is:" + getStr,
Toast. LENGTH_SHORT). show ();
OkButton. setOnClickListener (new okButtonListen ());

}

3. In SecondActivity, the listener can press OK and return to MainActivity. At the same time, the listener closes and sends data to MainActivity. The OK button listening code is as follows:
Copy codeThe Code is as follows: class okButtonListen implements OnClickListener {
Public void onClick (View v ){
// TODO Auto-generated method stub
Intent sendIntent = new Intent (SecondActivity. this, MainActivity. class); // this method was learned today and noted down! This is easy to write. I have some tutorials, And this part is not bound to Inent.
Bundle bundle = new Bundle ();
Bundle. putString ("send", "Hello everyone ");
SendIntent. putExtras (bundle );
SecondActivity. this. setResult (RESULT_ OK, sendIntent );
SecondActivity. this. finish ();
}

4. After return to main, main receives data from second. In MainActivity, repeat its OnActivityResult method.
Copy codeThe Code is as follows: @ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
// TODO Auto-generated method stub
Super. onActivityResult (requestCode, resultCode, data );
If (resultCode = RESULT_ OK ){
Bundle bundle = data. getExtras ();
String str = bundle. getString ("send ");
Toast. makeText (MainActivity. this,
"I came back. The data returned by the second activity is:" + str,
Toast. LENGTH_SHORT). show ();
}
}

Note:Do not create Intent again. onActivityResult has three parameters. The third parameter is Intent. You only need to use it as a parameter.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.