Return refresh of pages in Android

Source: Internet
Author: User

Android returns a activity after opening B activity from a activity, and a activity state change will be used to refresh

Let me introduce the most common methods of my development.

Reference function Setresult and onactivityresult processing mechanism

To implement the principle of returning data in the return event of the second activity, the first activity reconstructs the Onactivityresult method, and in the event of refactoring the method, feels the data returned, making different processing. To achieve the effect of automatically updating the first Activity . For more information, refer to the following description. currently two activity,mainacivity and secondactivity;

1 //important code for a mainacivity:2     Public voidOnClick (View v) {3Intent Intent =NewIntent ();4Intent.setclass (mainactivity. This, Secondactivity.class);5Bundle bundle =NewBundle ();6LOG.V ("ANDROID.ABC", "!!!!!" +mainactivity. This. toString ());7Bundle.putstring ("UID", mainactivity. This. toString ());8 Intent.putextras (bundle);9 Startactivityforresult (intent,get_code);Ten    } One   A //b.secondactivity, in Onclicklistener -Findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () -     {    the @Override -        Public voidOnClick (View v) -        {    -Setresult (RESULT_OK, (NewIntent ()). Setaction (UID));  + finish ();  -        }    +    });  A   at   //B. In mainactivity, rewrite the Onactivityresult method; - @Override -    protected voidOnactivityresult (intRequestcode,intResultCode, Intent data) -    {    -     if(Requestcode = =Get_code) -     {    in      if(ResultCode = =result_canceled) -       {    toText2.settext ("clicked Back");  +       }  -       Else  the       {    *         if(Data! =NULL)  $          {   Panax NotoginsengText2.settext ("Results returned by the second activity: \ n" +data.getaction ());  -           }    the       }    +     }    A}

where static final private int get_code = 0; Is my custom constant to distinguish which activity is currently returned, and the following is the code for class Secondactivity:

 Public classSecondactivityextendsactivity{string uid; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);      Setcontentview (R.layout.second); Bundle Bundle= This. Getintent (). Getextras (); if(Bundle! =NULL) {UID= bundle.getstring ("UID"); } Findviewbyid (R.id.button). Setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {Setresult (RESULT_OK, (NewIntent ()). Setaction (UID));           Finish ();    }       }); }}

Return refresh of pages in Android

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.