(Android review) Open the Activity and return the result. androidactivity

Source: Internet
Author: User

(Android review) Open the Activity and return the result. androidactivity

I. Basic knowledge points

In fact, it is easy to complete this function:

1. MainActivity

StartActivityForResult (intent, 100); // The second is the request code.


@ Override
Protected void onActivityResult (int requestCode, int resultCode, Intent data ){
Super. onActivityResult (requestCode, resultCode, data );

If (data! = Null ){
If (resultCode = 200) {// can it be determined based on the request or the result code?

String username = data. getStringExtra ("username ");
Et_username.setText (username );
}
}
}


2. MainActivity2


SetResult (200, intent); // The first parameter is the return code.



Ii. Code

1. MainActivity

Package com. example. setresult; import android. OS. bundle; import android. app. activity; import android. content. intent; import android. view. menu; import android. view. view; import android. widget. editText; public class MainActivity extends Activity {public EditText et_username; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); et_userna Me = (EditText) findViewById (R. id. et_name);} public void get (View view) {Intent intent = new Intent (this, MainActivity2.class); startActivityForResult (intent, 100 ); // The second is the Request Code} @ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {super. onActivityResult (requestCode, resultCode, data); if (data! = Null) {if (resultCode = 200) {// can be determined based on the request or the result code? String username = data. getStringExtra ("username"); et_username.setText (username) ;}}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


2. MainActivity2

Package com. example. setresult; import android. app. activity; import android. content. intent; import android. graphics. color; import android. OS. bundle; import android. view. textureView; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. baseAdapter; import android. widget. listView; import android. wid Get. textView; public class MainActivity2 extends Activity {public ListView listView; public MyAdapter adapter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main2); listView = (ListView) findViewById (R. id. listview); adapter = new MyAdapter (); listView. setAdapter (adapter); listView. setOnItemClickListener (new MyOnItemClickListener ()) ;} Public class MyOnItemClickListener implements OnItemClickListener {@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int position, long arg3) {Intent intent = new Intent (getApplicationContext (), MainActivity. class); String username = (String) adapter. getItem (position); intent. putExtra ("username", username); setResult (200, intent); // The first parameter is the return code finish (); // end the current activity} public class MyAdapter extends BaseAdapter {String names [] = new String [] {"hjd", "hjd1", "hjd2 ", "hjd3", "hjd4"}; @ Overridepublic int getCount () {return names. length ;}@ Overridepublic Object getItem (int position) {return names [position] ;}@ Overridepublic long getItemId (int position) {return position ;}@ Overridepublic View getView (int position, view arg1, ViewGroup arg2) {TextView TV = new TextView (getApplicationContext (); TV. setText (names [position]); TV. setTextSize (25); TV. setTextColor (Color. GREEN); return TV ;}}}


3. Download source code

Http://download.csdn.net/detail/caihongshijie6/7793215


IV,






Android presses the return key in an Activity. Instead of finishing the Activity, it returns to the previous page. How can this problem be solved?

Do you mean communication between two activities?
If so, there is a method in the main Activity: startActivityForResult (intent, 100 );
Call setResult (200, intent) in the event of the return button on the next access page );
Intent is the parameter used to access two activities and is passed with intent.
The numbers 100, 200, and are all response parameters used for accessing two pages on the home page and accessing one page by different home pages.
Override onActivityResult (int requestCode, int resultCode, Intent data) in the main Activity to process the page access result ~~

How can I set a return button in android and click it to return to the previous activity?

Button. setOnClickListener (mGoBack );
Public OnClickListener mGoBack = new OnClickListener (){

Public void onClick (View v ){
Finish ();
}
};
To return the previous activity, you only need to end the current activity.

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.