Android Development notes (8) -- Call sub-Activity, androidactivity
Reprinted Please note: http://www.cnblogs.com/igoslly/p/6853730.html
Call a sub-Activity
| The return value of a child Activity is required. |
MainActivity uses startActivityForResult Method |
| The return value of a child Activity is not required. |
MainActivity uses startActivity Method |
| MainActivity |
Set intent. putExtras directly |
MainActivity When passing values:
Intent intent = new Intent (MainActivity. this, subActivity. class); intent. putExtra ("value_key", transmitted value)
Value_key is a value tag agreed by the developer. It can be obtained through getIntExtra in the sub-Activity (when transmitted as int ). PutExtra can transmit multiple types of values, but the get function is slightly different.
Intent intent = getIntent();intent.getIntExtra(“value_key”,0);
The sub-function obtains the corresponding value based on the different labels set for each value. When startActivityForResult is used
Intent intent =new Intent(MainActivity.this,subActivity.class);startActivityForResult(intent,0x1001);
0x1001 is the custom request code. In the sub-Activity, setResult () is used to set the return value.
Intent. intent = new Intent (); intent. putExtra ("revalue_key", return value); setResult (0x1001, intent );When the child Activity is disabled, MainActivity starts the onActvityResult method.
@overridesuper.onActivityResult(requestCode,resultCode,data){ if(requestCode==0x1001){ String str =data.getStringExtra(“revalue_key”);} }RequestCode is the promoter Activity. resultCode displays the running results of the sub-Activity, usually Activity. RESULT_ OK (normal) and Activity. RESULT_CANCELLED (abnormal shutdown); data is returned by an Intent package.
Statement:
1. The text and pictures in this note are both original and original. For reprinted documents, enter "blog garden-igoslly ".
2, Android development courses in 2017 4 years to participate in Google developer learning, notes original http://www.studyjamscn.com/thread-20580-1-1.html#pid272486