1. Demo description and demonstration
This demo demonstrates how to use startactivityforresult to get a returned result from the started activity. The main lessons learned are as follows:
- Startactivityforresult
- Functions of Request Code and result code
The demo is as follows:
2. Video description
Http://www.eyeandroid.com/thread-10471-1-1.html
3. Demo Analysis
Startactivity can start a new activity. The new activity is relatively independent from the activity that starts it and does not return the results of the activity that calls it. When writing an application, you sometimes need to return results from other activities. Receive result shows this usage.
Startactivityforresult is similar to startactivity. It is important that startactivityforresult can return results from the called activity. Get_code is the request code. When a sub-activity returns results, the request code can be used to process the results returned by the activity.
- // Definition of the one requestcode we use for processing ing resuls.
- Static final private int get_code = 0;
- Private onclicklistener mgetlistener = new onclicklistener (){
- Public void onclick (view v ){
- // Start the activity whose result we want to retrieve.
- // Result will come back with Request Code get_code.
- Intent intent = new intent (receiveresult. This, sendresult. Class );
- Startactivityforresult (intent, get_code );
- }
- };
When the child activity ends, call setresult to set the returned result. Two activities are involved in the receive result example. receiveresult is the parent activity, sendresult is the user option returned by the child activity, and the returned result is displayed in receiveresult.
The Code returned by sendresult is as follows:
- Private onclicklistener mcorkylistener = new onclicklistener ()
- {
- Public void onclick (view V)
- {
- // To send a result, simply call setresult () before your
- // Activity is finished.
- Setresult (result_ OK, (new intent (). setaction ("Corky! "));
- Finish ();
- }
- };
- Private onclicklistener mviletlistener = new onclicklistener ()
- {
- Public void onclick (view V)
- {
- // To send a result, simply call setresult () before your
- // Activity is finished.
- Setresult (result_ OK, (new intent (). setaction ("Violet! "));
- Finish ();
- }
- };
After a Child Activity returns results, the parent activity processes the results returned by the Child Activity by defining onactivityresult.
- Protected void onactivityresult (INT requestcode, int resultcode,
- Intent data ){
- // You can use the requestcode to select between multiple child
- // Activities you may have started. Here there is only one thing
- // We launch.
- If (requestcode = get_code ){
- // We will be adding to our text.
- ...
- }
- }
Three parameters in onactivityresult:
Requestcode: The request code when startactivityforresult is called. You can use the request code to distinguish multiple sub-acitifiers.
Resultcode: return value of the sub-activity, which is generally result_ OK (normal) and result_cancelled (canceled ).
Data: return result of activity. In this example, we use the selected Corky or violet.
4. We learned this demo.
1. Usage of startactivityforresult
Http://www.eyeandroid.com/thread-10257-1-1.html
2. Android textview. buffertype instance description
Http://www.eyeandroid.com/thread-9134-1-1.html