Problem with onActivityResult

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with onActivityResult

Postby fabiel prestes » Wed Nov 05, 2008 11:25 am

Hi all,

I have a problem with onActivityResult. My class Activity can not return a result for this method.

My class A
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class ClassTestA {
  3.  
  4.         private Button btOk;
  5.  
  6.         private Intent bus;
  7.  
  8.  
  9.  
  10.         /** Called when the activity is first created. */
  11.  
  12.         @Override
  13.  
  14.         public void onCreate(Bundle savedInstanceState) {
  15.  
  16.                 super.onCreate(savedInstanceState);
  17.  
  18.                 setContentView(R.layout.main);
  19.  
  20.  
  21.  
  22.                 btOk = (Button) findViewById(R.id.btTeste);
  23.  
  24.                 bus = new Intent(AndTeste.this, Buscador.class);
  25.  
  26.  
  27.  
  28.                 btOk.setOnClickListener(new OnClickListener() {
  29.  
  30.  
  31.  
  32.                         public void onClick(View v) {
  33.  
  34.                                 bus = new Intent(AndTeste.this, Buscador.class);
  35.  
  36.                                 startActivityForResult(bus, RESULT_OK);
  37.  
  38.                         }
  39.  
  40.  
  41.  
  42.                 });
  43.  
  44.         }
  45.  
  46.  
  47.  
  48.         // Listen for results.
  49.  
  50.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  51.  
  52.                 // See which child activity is calling us back.
  53.  
  54.                 switch (resultCode) {
  55.  
  56.                    case RESULT_OK:
  57.  
  58.                         setTitle("Result Ok");
  59.  
  60.                    default:
  61.  
  62.                         break;
  63.  
  64.                 }
  65.  
  66.         }
  67.  
  68. }
  69.  
  70.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Class B
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class ClassB {
  3.  
  4.         private Button btBus;
  5.  
  6.         private EditText end;
  7.  
  8.         private Geocoder buscador;
  9.  
  10.         private ListView list;
  11.  
  12.        
  13.  
  14.         /** Called when the activity is first created. */
  15.  
  16.     @Override
  17.  
  18.     public void onCreate(Bundle savedInstanceState) {
  19.  
  20.         super.onCreate(savedInstanceState);
  21.  
  22.         setContentView(R.layout.buscador);
  23.  
  24.        
  25.  
  26.         btBus = (Button) findViewById(R.id.btBuscar);
  27.  
  28.         end = (EditText) findViewById(R.id.tiEnd);
  29.  
  30.         buscador = new Geocoder(this);
  31.  
  32.         list = (ListView) findViewById(R.id.listEnd);
  33.  
  34.        
  35.  
  36.         btBus.setOnClickListener(mCorkyListener);
  37.  
  38.     }
  39.  
  40.  
  41.  
  42.     private OnClickListener mCorkyListener = new OnClickListener() {
  43.  
  44.         public void onClick(View v) {
  45.  
  46.             setResult(RESULT_OK);
  47.  
  48.             finish();
  49.  
  50.         }
  51.  
  52.     };
  53.  
  54. }
  55.  
  56.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


Manifest.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      package="com.maps"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0.0">
  11.  
  12.      
  13.  
  14.     <application android:icon="@drawable/icon" android:label="testeando tudo">
  15.  
  16.         <activity android:name=".ClassA" android:label="Testeando a And">
  17.  
  18.             <intent-filter>
  19.  
  20.                 <action android:name="android.intent.action.MAIN" />
  21.  
  22.                 <category android:name="android.intent.category.LAUNCHER" />
  23.  
  24.             </intent-filter>
  25.  
  26.         </activity>
  27.  
  28.        
  29.  
  30.         <activity android:name=".ClassB" android:label="TESTEANDO">
  31.  
  32.             <intent-filter>
  33.  
  34.                 <action android:name="android.intent.action.VIEW" />
  35.  
  36.                 <category android:name="android.intent.category.DEFAULT" />
  37.  
  38.             </intent-filter>
  39.  
  40.         </activity>
  41.  
  42.     </application>
  43.  
  44. </manifest>
  45.  
  46.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4

My class B to return to class A, but o method onActivityResult is not called.
Someone can help me ?
fabiel prestes
Freshman
Freshman
 
Posts: 4
Joined: Mon Nov 03, 2008 1:11 pm

Top

Postby MrSnowflake » Wed Nov 05, 2008 11:59 am

I'm not very sure, but I guess you need the requestCode and not the responseCode:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. switch (requestCode) {
  2.  
  3.   case RESULT_OK:
  4.  
  5.     setTitle("Result Ok");
  6.  
  7.   default:
  8.  
  9.     break;
  10.  
  11. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby Premier » Fri Jul 31, 2009 10:08 pm

I used your source code, but onActivityResult is called before onCreate in class B is terminated, why?
in lifecycle, onActivityResult is called after setResult and finish in class B ?
Premier
Junior Developer
Junior Developer
 
Posts: 21
Joined: Fri Jul 10, 2009 4:04 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests