the difference between Startactivityforresult and StartActivity is that:
1, StartActivity ()
Just jump to the target page, and if you want to jump back to the current page, you must use StartActivity () once more.
2, Startactivityforresult ()
This task can be done one time, when the program executes to this code, if from T1activity jump to the next text2activity, and when the text2activity call the Finish () method, the program will automatically jump back t1activity , and call the Onactivityresult () method in the previous t1activity.
Related functions:
Startactivityforresult (Intent Intent, Int requestcode)
Setresut (int resultcode, Intent Intent)
Onactivityresult (int requestcode, int resultcode, Intent Intent)
Simple examples are:
1. Jump is not using startactivity (intent) This method, but Startactivityforresult (intent, 0)
Intent intent=new Intent (); Intent.setclass (A.this, B.class); Bundle Bundle=new Bundle (); String str1= "aaaaaa"; bundle.putstring ("str1", str1); Intent.putextras (bundle); Startactivityforresult (intent, 0);// Here use Startactivityforresult to do jump, here 0 is a basis, can write other values, but must >=0
2. Rewrite the Onactivityresult method to receive data from B callbacks.
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {switch (ResultCode) {//resultcode is a callback token, I The callback in B is RESULT_OK case RESULT_OK: Bundle B=data.getextras ();//data is the intent String str=b.getstring for callbacks in B ( "str1");//str is the value of the callback Break;default: Break ; }}
3. Use the Setresult method when returning data in B, and then call the Finish method.
Setresult (RESULT_OK, intent); Intent is a bundle of intent, of course, you can also define a new bundlefinish ();//You must call the Finish () method here
When the Android activity Setresult () is called (emphasis is also difficult)
If Setresult is set in the Startactivityforresult activity, the result is not immediately returned to the parent's activity, only the current activity is finish, The result will be sent to the parent's onactivityresult to deal with!
If an activity is going to return data to the activity that initiated it, you can call the Setresult () method. When do I call the Setresult () method to return the data?
Look at the source code will understand:
Public final void Setresult (Int. ResultCode, Intent data) { synchronized (this) { mresultcode = ResultCode; Mresultdata = data; } } public void Finish () { if (mparent = = null) { int resultcode; Intent Resultdata; Synchronized (this) { resultcode = Mresultcode; Resultdata = Mresultdata; } if (CONFIG.LOGV) log.v (TAG, "finishing self:token=" + Mtoken); try { if (Activitymanagernative.getdefault () . Finishactivity (Mtoken, ResultCode, Resultdata)) { Mfinished = true; } } catch (RemoteException e) { //Empty } }} else { mparent.finishfromchild (this);} }
This code shows that the activity returns result at the time of the finish, which means that the call to the Setresult () method must precede the finish ().
If you call Setresult () in the following method, it may not return success: OnPause (), OnStop (), OnDestroy (),
Because these method calls are not necessarily before the finish, of course OnCreate () on the call Setresult must be before finish
Press the back key to exit from an activity, a press back,android will automatically invoke the activity's finish () method, and then set ResultCode to Result_canceled, will not return any data.
The solution is to capture the events in the activity by the back, capture the first Setresult, and then call finish, then it's done ... To swallow the back incident directly.
@Override public void onbackpressed () { log.i (TAG, "onbackpressed"); Setresult (CONST.LIVE_OK); Super.onbackpressed (); }
Of course you can call Setresult in OnCreate (), but I don't think this method is good enough to rewrite the onbackpressed () method.
Introduction to Android Setresult and Onactivityforresult