Explicit Intent For Result Problem

Put your problem here if it does not fit any of the other categories.

Explicit Intent For Result Problem

Postby grimx » Mon Dec 28, 2009 10:19 pm

What i'm tring to do is ParentActivity start SubActivity forResult on the push of the Open button
and thats where it force closes.
Help.


ParentActivity.java
Code: Select all
package com.grimx.ParentActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class ParentActivity extends Activity implements View.OnClickListener{
   private static final int SHOW_SUBACTIVITY = 1;
    Button open_btn;
    TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        textView = (TextView)findViewById(R.id.textView);
        open_btn = (Button)findViewById(R.id.open);
        open_btn.setOnClickListener(this);
    }

   @Override
   public void onClick(View v) {
      if(v.getId() == R.id.open) {
         textView.setText("Open Button Was Clicked");
         Intent my_intent = new Intent(ParentActivity.this, SubActivity.class);
         startActivityForResult(my_intent, SHOW_SUBACTIVITY);
         //startActivity(my_intent);
      }
   }
   
   

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      
      super.onActivityResult(requestCode, resultCode, data);
      
      switch(requestCode) {
         case(SHOW_SUBACTIVITY):
         {
            if(resultCode == Activity.RESULT_OK) {
            String stuff = data.getDataString();
            textView.setText(stuff);
            }   
         }
         break;
         }
   }
}



SubActivity
Code: Select all
package com.grimx.ParentActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class SubActivity extends Activity implements View.OnClickListener{
   protected final int RETURN_CODE = 1;
   Button send_btn;
   TextView textView;
   
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub);
       
        textView = (TextView)findViewById(R.id.textView);
        send_btn = (Button)findViewById(R.id.send);
        send_btn.setOnCreateContextMenuListener(this);
       
    }

   @Override
   public void onClick(View v) {
      String data = null;
      
      if(v.getId() == R.id.send) {
         //data = (String)textView.getText();
         Intent myIntent = new Intent();
         myIntent.putExtra("data", textView.getText().toString());
         
         setResult(RETURN_CODE, myIntent);
         finish();
      }
   }

}


AndroidManifest.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.grimx.ParentActivity"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0">
  11.  
  12.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  13.  
  14.         <activity android:name=".ParentActivity"
  15.  
  16.                  android:label="@string/app_name">
  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.         <activity android:name=".SubActivity"
  29.  
  30.                         android:label="@string/sub_name">
  31.  
  32.         </activity>
  33.  
  34.     </application>
  35.  
  36.     <uses-sdk android:minSdkVersion="3" />
  37.  
  38.  
  39.  
  40. </manifest>
  41.  
  42.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
grimx
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat Dec 05, 2009 12:47 am

Top

Postby grimx » Mon Dec 28, 2009 10:22 pm

Sorry for the double post
when i clicked submit, i hit it twice.
grimx
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat Dec 05, 2009 12:47 am

Postby grimx » Tue Dec 29, 2009 2:45 am

Ok i fixed most of it, now my problem is tring to return a string from SubActivity.

ParentActivity
Code: Select all
package com.grimx.ParentActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;

public class ParentActivity extends Activity implements View.OnClickListener{
   protected static final int SHOW_SUBACTIVITY = 101;
    Button open_btn;
    TextView textView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        textView = (TextView)findViewById(R.id.textView);
        open_btn = (Button)findViewById(R.id.open);
        open_btn.setOnClickListener(this);
    }

   @Override
   public void onClick(View v) {
      if(v.getId() == R.id.open) {
         textView.setText("Open Button Was Clicked");
         Intent my_intent = new Intent(ParentActivity.this, SubActivity.class);
         startActivityForResult(my_intent, SHOW_SUBACTIVITY);
      }
   }

   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      
      super.onActivityResult(requestCode, resultCode, data);
      String stuff = null;
      if(requestCode == SHOW_SUBACTIVITY){
         textView.setText("return value: " + data.getDataString());
      }
      else {
         textView.setText("DID NOT GET YOUR INPUT!!!!!");
      }
   }
}


SubActivity
Code: Select all
package com.grimx.ParentActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;

public class SubActivity extends Activity implements View.OnClickListener{
   protected static final int RETURN_CODE = 201;
   Button send_btn;
   EditText textEdit;
   
   public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sub);
       
        textEdit = (EditText)findViewById(R.id.textView);
        send_btn = (Button)findViewById(R.id.send);
        send_btn.setOnClickListener(this);
       
    }

   @Override
   public void onClick(View v) {
      String str = null;
      
      if(v.getId() == R.id.send) {
         str = textEdit.getText().toString();
         Intent myIntent = new Intent(str);
         setResult(RETURN_CODE, myIntent);
         finish();
      }
   }

}


AndroidManifest
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.grimx.ParentActivity"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0">
  11.  
  12.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  13.  
  14.         <activity android:name=".ParentActivity"
  15.  
  16.                  android:label="@string/app_name">
  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.         <activity android:name=".SubActivity"
  29.  
  30.                         android:label="@string/sub_name">
  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.     <uses-sdk android:minSdkVersion="3" />
  45.  
  46.  
  47.  
  48. </manifest>
  49.  
  50.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
[syntax="xml"]
grimx
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat Dec 05, 2009 12:47 am

Postby grimx » Tue Dec 29, 2009 3:48 am

ok i got.
Problem Solved.

i needed this in ParentActivity:

String stuff = null;
switch(requestCode) {
case SHOW_SUBACTIVITY:
{
if(resultCode == RESULT_OK) {
stuff = data.getStringExtra("Return"); // "Return" being the name of the extra data.
textView.setText("your returned value: " + stuff);
}

break;
}
}




and needed this in SubActivity:

str = textEdit.getText().toString(); // Get the string from the EditText
Intent myIntent = new Intent();
myIntent.putExtra("Return", str); // Give the the data to be returned a name "Return" along with the data it's self.
// Inside ParentActivity use this name "Return" to get the extra data .

It works, so do i undestand this correctly?
grimx
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat Dec 05, 2009 12:47 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests