Call startActivityForResult to start the activity, and return the problem that the current page does not respond (with the activity carrying the parameter process), startactivityresult

Source: Internet
Author: User

Call startActivityForResult to start the activity, and return the problem that the current page does not respond (with the activity carrying the parameter process), startactivityresult

Recently I encountered such a problem in the project. The original activity was not written for me. I want to change it to return the parameter carried by the activity. After modification, I found that onActivityResult cannot be called. There is no problem with debugging. I also use the finish function at the end of the activity. In this case, the Manifest activity Statement is not found. Next we will talk about the problem of no response.

1. The start mode of Manifest configuration is related

Activity is related to the start mode configured by Manifest. do not configure the start mode. android: launchMode = "singleTask ". The reason is in AndroidManifest. on the page jumped to in xml, I set android: launchMode = "singleTask", because the Activity that needs to pass the value cannot set this attribute or singleInstance, or it can only be set to standard mode, otherwise, onActivityResult () will be called directly after startActivityForResult (). In addition, the requestCode value must be greater than or equal to 0. Otherwise, startActivityForResult becomes startactivity.

2. Press the return key to call the finish function.

In B, it must be setResult (), call finish (), and return to A. A will automatically call onActivityResult ()
If you press Back directly, it will not be called.

StartActivityForResult (intent, 100); // This sentence starts activityIntent intentSend = new Intent (); // when return, set Bundle sendBundle = new Bundle (); intentSend. putExtras (sendBundle); setResult (RESULT_ OK, intentSend); finish (); // The Return key calls public boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stubif (keyCode = KeyEvent. KEYCODE_BACK) {finish ();} return true ;}

Iii. General Procedure

The process is that Aactivity enters Bactivity. Both the incoming and returned results carry data, and then refresh Aactivity. There must be a flag, that is, startActivityForResult (newIntent, 12); this sentence is obvious, starting a new activity with a result.

1. itent Aactivity enters Bactivity and uses Bundle to carry data, marking 12

Intent newIntent = new Intent();newIntent.setClass(this, TActivity.class);Bundle sentBundle = new Bundle();sentBundle.putString("Template", String.format("%.4f", 555));newIntent.putExtras(sentBundle);startActivityForResult(newIntent, 12);
2. Access the data carried on the new Bactivity.

Bundle bundle = this.getIntent().getExtras();if (bundle==null) {return;}                             bundle.getString("Template");

3. When Bactivity returns, it carries data to Aactivity.
Intent intentSend = new Intent (); Bundle bundleSend = new Bundle (); bundleSend. putString ("Template", "data"); intentSend. putExtras (bundleSend); setResult (RESULT_ OK, intentSend); finish ();
4. Aactivity receives data when it returns
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent dataReceive) {if (dataReceive == null)return;if (requestCode == 12) {Bundle bundleReceive = dataReceive.getExtras();if (bundleReceive == null)  return;bundleReceive.getString("Template");}super.onActivityResult(requestCode, resultCode, dataReceive);}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.