Callback Setresult considerations in Startactivityforresult

Source: Internet
Author: User

Thoughts on reading Http://www.cnblogs.com/lijunamneg/archive/2013/02/05/2892616.html

In this paper, a core question is raised:

When did Android activity Setresult () call?

He gave the answer:

Activity returns result at the time of the finish, which means that the call to Setresult () method must precede 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

What does that mean?

Suppose we have mainactivity and secondactivity to jump at once.

Package Com.example.setresult;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.content.Intent;import Android.os.bundle;import Android.util.log;import Android.view.layoutinflater;import Android.view.Menu;import Android.view.menuitem;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.button;import Android.os.build;public class MainActivity extends actionbaractivity {Private final static String TAG = MainActivity.class.getSimpleName (); final int requestcode=1;    Button B1;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        b1= (Button) Findviewbyid (R.ID.B1); B1.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {startactivityforresult (New Intent ( Mainactivity.this,secondactivity.class), Requestcode);}); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent arg2) {log.d (TAG, "Resul      Tcode= "+resultcode);              Switch (resultcode) {case RESULT_OK:LOG.D (TAG, "OK");        Break    } super.onactivityresult (Requestcode, ResultCode, arg2);    } @Override protected void Onrestart () {LOG.D (TAG, "mainactivity Onrestart");    Super.onrestart ();    } @Override protected void Onresume () {LOG.D (TAG, "mainactivity onresume");    Super.onresume (); }}

Package Com.example.setresult;import Android.support.v7.app.actionbaractivity;import Android.support.v7.app.actionbar;import Android.support.v4.app.fragment;import Android.os.Bundle;import Android.util.log;import Android.view.layoutinflater;import Android.view.menu;import Android.view.MenuItem;import Android.view.view;import Android.view.viewgroup;import Android.os.build;public class SecondActivity extends actionbaractivity {Private final static String TAG = SecondActivity.class.getSimpleName (); @Overrideprotected void ONCR Eate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_second); Setresult (RESULT_OK);} @Overrideprotected void OnStop () {LOG.D (TAG, "secondactivity onStop"); Super.onstop ();} @Overrideprotected void OnDestroy () {LOG.D (TAG, "secondactivity OnDestroy"); Super.ondestroy ();} @Overridepublic void onbackpressed () {LOG.D (TAG, "secondactivity onbackpressed"); super.onbackpressed ();} @Overrideprotected void OnStart () {LOG.D (TAG, "SeconDactivity OnStart "); Super.onstart ();} @Overrideprotected void OnPause () {LOG.D (TAG, "secondactivity onPause"); Super.onpause ();}}

  


07-30 20:15:37.291:d/secondactivity (30684): Secondactivity OnStart
07-30 20:15:39.151:d/secondactivity (30684): Secondactivity onbackpressed

07-30 20:25:18.531:d/secondactivity (32594): Secondactivity onPause

07-30 20:15:39.161:d/mainactivity (30684): Resultcode=-1
07-30 20:15:39.161:d/mainactivity (30684): RESULT_OK
07-30 20:15:39.161:d/mainactivity (30684): Mainactivity Onrestart
07-30 20:15:39.161:d/mainactivity (30684): Mainactivity onresume
07-30 20:15:39.501:d/secondactivity (30684): Secondactivity onStop
07-30 20:15:39.501:d/secondactivity (30684): Secondactivity OnDestroy

The above log occurs when the return key is pressed from secondactivity. You can see that resultcode=-1 is right.

But when you put

Setresult (RESULT_OK);

This sentence is put in secondactivity Onstop ()

The following log will occur:


07-30 20:19:31.521:d/secondactivity (31785): Secondactivity OnStart
07-30 20:19:35.031:d/secondactivity (31785): Secondactivity onbackpressed

07-30 20:25:18.531:d/secondactivity (32594): Secondactivity onPause

07-30 20:19:35.051:d/mainactivity (31785): resultcode=0
07-30 20:19:35.051:d/mainactivity (31785): Mainactivity Onrestart
07-30 20:19:35.051:d/mainactivity (31785): Mainactivity onresume
07-30 20:19:35.381:d/secondactivity (31785): Secondactivity onStop
07-30 20:19:35.381:d/secondactivity (31785): Secondactivity OnDestroy

Resultcode=0 Description Setresult Not successful

It can be seen that when the Onbackpressed event occurs, the activity will directly drop the finish () method, when ResultCode has returned to the previous activity, so the default value resultcode=0

A conclusion is drawn from this

Setresult () call must be preceded by OnPause ()

Callback Setresult considerations in Startactivityforresult

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.