Android onactivityresult callback method example

Source: Internet
Author: User
Package COM. example. activityforresult; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. util. log; import android. view. menu; import android. view. view; import android. widget. button; public class mainactivity extends activity {public static int result_ OK = 5; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); button mfirstbutton = (button) findviewbyid (R. id. first); button secondbutton = (button) findviewbyid (R. id. second); mfirstbutton. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {gotofirstactivity () ;}}); secondbutton. setonclicklistener (new view. onclicklistener () {@ overridepublic void onclick (view v) {gotosencondactivity () ;}}) ;} private void gotofirstactivity () {startactivityforresult (new intent (mainactivity. this, firstactivity. class), 1);} private void gotosencondactivity () {startactivityforresult (new intent (mainactivity. this, secondactivity. class), 2) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. activity_main, menu); Return true;}/*** function name: onactivityresult Function Description: callback function parameter description: ** @ Param requestcode * @ Param resultcode * @ Param data * return value: ** modify record: Date: 2:24:25 modifier: Description: **/@ overrideprotected void onactivityresult (INT requestcode, int resultcode, intent data) {Switch (requestcode) {Case 1: // todoif (resultcode = result_ OK) {string getdata = data. getstringextra ("first"); log. D ("tag", getdata);} break; Case 2: // todoif (resultcode = result_ OK) {string getdata = data. getstringextra ("second"); log. D ("tag", getdata) ;}break; default: Break ;}}}

  

package com.example.activityforresult;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;public class FirstActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.first_activity);Button button = (Button) findViewById(R.id.resultbutton1);button.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){Intent intent = new Intent();intent.putExtra("first", "first");setResult(MainActivity.RESULT_OK, intent);finish();}});}}

  

Related Article

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.