SubActivites with return value - The InputBox

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Postby plusminus » Mon Feb 04, 2008 12:35 pm

Hello venkat,

we have a tutorial on :src: Passing Data to (Sub)Activities.

If I have some final values I prefer to store them in a Interface. Sample from AndNav:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public interface Constants {
  2.         /** Overall DebugTag for whole AndNav. */
  3.         public final String DEBUGTAG = "ANDNAV_DEBUGTAG";
  4.        
  5.         /** Shall be used if only one SubActivity is closed.*/
  6.         public final int SUBACTIVITY_RESULTCODE_UP_ONE_LEVEL = 0;
  7.         /** Shall be used if a chain of SubActivities is to be closed.*/
  8.         public final int SUBACTIVITY_RESULTCODE_CHAINCLOSE = SUBACTIVITY_RESULTCODE_UP_ONE_LEVEL + 1;
  9.         /** Shall be used to go back one level in the dialog-process. */
  10.         public final int SUBACTIVITY_RESULTCODE_SUCCESS = SUBACTIVITY_RESULTCODE_CHAINCLOSE + 1;
  11. }
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby venkat » Mon Feb 04, 2008 12:47 pm

thanks for your replay plusminus,
actually i need to pass data "from activity to subacitivity" not "subactivity to activity" :)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby plusminus » Mon Feb 04, 2008 12:54 pm

Hello venkat,

the Tutorial description is : "Passing data to SubActivities"

I extracted relevant lines for you:
MainActivity (perhaps on Button Click):
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                     Intent i = new Intent(MainActivity.this,
  2.  
  3.                                              SubActivityWithResult.class);
  4.  
  5.                    
  6.  
  7.                     /* Create a bundle that will  
  8.  
  9.                      * hold will be passed to the
  10.  
  11.                      * SubActivityover the Intent */
  12.  
  13.                     Bundle b = new Bundle();
  14.  
  15.                     b.putString("DEFAULTTEXT", "Some Default blabla...");
  16.  
  17.                     i.putExtras(b);
  18.  
  19.                     // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
  20.  
  21.                     startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);
Parsed in 0.091 seconds, using GeSHi 1.0.8.4

and with the SubActivity do:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    // ...
  2.  
  3.     @Override
  4.  
  5.     public void onCreate(Bundle icicle) {
  6.  
  7.         super.onCreate(icicle);
  8.  
  9.         // Apply our subactivity.xml-Layout
  10.  
  11.         setContentView(R.layout.subactivity);
  12.  
  13.        
  14.  
  15.         /* Retrieve the String from the Extra-Bundle
  16.  
  17.          * we started this SubActivity with. */
  18.  
  19.         Bundle b = this.getIntent().getExtras();
  20.  
  21.         String s = b.getString("DEFAULTTEXT");
  22.  
  23.         // Find the EditText defined in the subactivity.xml
  24.  
  25.         ((EditText)findViewById(R.id.subactivity_edit_returnvalue)).setText(s);
  26.  
  27.  
  28.  
  29.         // ...
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby venkat » Mon Feb 04, 2008 1:09 pm

Thank you very much Plus minus :) ..
i will try it now and let you know if i have any doubt.
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby venkat » Mon Feb 04, 2008 1:26 pm

it works :)

thank you very much.. :) ;)
Regards,
Venkat.
venkat
Senior Developer
Senior Developer
 
Posts: 152
Joined: Tue Nov 27, 2007 5:42 am
Location: India

Postby bavarol » Thu May 29, 2008 10:51 am

Hi

I have understood the way you pass primitive Type objects but it's not so clear how it can pass a more complex Object between Activities.

It should be something like this, shouldn't it?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Intent intent = new Intent(InvokingActivity.this, InvokedActivity.class);
  2. intent.putExtra("anObject",object) ;
  3. startSubActivity(intent, 0);
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Bundle bundle = this.getIntent().getExtras();
  2. if ( bundle != null ) {
  3.         Object object = bundle.get("anObject"); <-- Error
  4. }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4

However I get an error :
Code: Select all
The Method get(String) is undefined for type Bundle


=============================================

How can I pass objects between activities?

Cheers


P.S.:I have the SDK m3rc22a.
bavarol
Experienced Developer
Experienced Developer
 
Posts: 52
Joined: Mon Dec 10, 2007 9:19 pm

Top

showAlert problem

Postby feelingAndroid » Wed Jul 02, 2008 4:23 am

hi plusminus,
This line is something wrong :showAlert("SubActivity returned",0, "ReturnValue: " + data,
"OK", false); Eclipse shows as follows:
The method showAlert(CharSequence, int, CharSequence, CharSequence, boolean) in the type
ApplicationContext is not applicable for the arguments (String, String, String, boolean)
Last edited by feelingAndroid on Fri Jul 04, 2008 4:58 am, edited 1 time in total.
feelingAndroid
Once Poster
Once Poster
 
Posts: 1
Joined: Mon May 26, 2008 8:01 am

Postby mrdanger » Thu Jul 03, 2008 6:16 pm

Hello everbody,

i get the failure message
"... List Views can't have unspecified size"

Can you help me?

About my Probleme and the solution
Google Discussion
mrdanger
Junior Developer
Junior Developer
 
Posts: 10
Joined: Tue Jun 17, 2008 3:22 pm
Location: Hamburg, Germany

Postby darolla » Tue Dec 30, 2008 1:40 pm

thanks for this. but in the actual sdk there are some changes.

here comes the clue :roll:

this way the subactivity is called:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Intent intent = new Intent();
  3.  
  4. intent.setClass(this, SubActivity.class);
  5.  
  6. startActivityForResult(intent, 1234);
  7.  
  8.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


this way the "result" is given back to the calling activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  5.  
  6.         super.onActivityResult(requestCode, resultCode, data);
  7.  
  8.  
  9.  
  10.         Log.d(TAG, "" + requestCode);
  11.  
  12.         Log.d(TAG, "" + resultCode);
  13.  
  14.         Log.d(TAG, "" + data.getStringExtra( "result" ) );
  15.  
  16. }      
  17.  
  18.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


this way the "result" is send in the subactivity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. Button someExitButton = (Button)findViewById(R.id.someExitButton);
  3.  
  4. someExitButton.setOnClickListener(new OnClickListener(){
  5.  
  6.     public void onClick(View view) {
  7.  
  8.         Intent data = new Intent();
  9.  
  10.         data.putExtra( "result", theResultString );
  11.  
  12.         setResult( RESULT_OK, data );
  13.  
  14.         finish();
  15.  
  16.     }
  17.  
  18. });                            
  19.  
  20.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


greetings,
darolla
User avatar
darolla
Master Developer
Master Developer
 
Posts: 273
Joined: Thu Sep 25, 2008 5:16 pm
Location: Dortmund, Germany

Postby Artur79 » Thu Jan 15, 2009 12:57 pm

a line (adjusted to orginal code)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. intent.setClass(this, SubActivityWithResult.class);
  3.  
  4.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


doesn't work
Artur79
Developer
Developer
 
Posts: 26
Joined: Fri Sep 26, 2008 12:53 pm

import

Postby ioRek » Mon Jun 22, 2009 3:36 pm

what's the import needed for showalert, still can't find it RHA
ioRek
Freshman
Freshman
 
Posts: 9
Joined: Mon Jun 22, 2009 3:10 pm

Postby xenon » Thu Jun 25, 2009 2:09 pm

Unfortunately, like Artur79 already mentioned, the following line doesn't work:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. intent.setClass(this, SubActivityWithResult.class);
  2.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Error message

The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments (new View.OnClickListener(){}, Class<SubActivityWithResult>) MainActivity.java subactivitywithresult/src/com/example/subactivitywithresult line 28 Java Problem



Someone knows how to fix this?
xenon
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 11, 2009 6:11 pm

Postby Erykgecko » Sun Jul 19, 2009 8:33 pm

ok, I have an activity that calls a subactivity, based on this code. so thats fine, it all works, i can call my subactivity, select something and it passes the data back to my activity... Groovy,

however, if i switch to my subactivity, then decide i don't want to select anything, i want to just return to my normal activity, i hit the back button on my phone.

this breaks my app... and i get the standard force close. So I figured i would try adding onStop() and onDestroy() to my sub activity, and in them just called the finish() that should pass it back to the main activity.. alas it will not work...

I'm sure theres a really simple answer i'm missing, but I can't seem to figure it out...

any suggestions?

-edit-

OK, Figured it,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    @Override
  2.  
  3.    public boolean onKeyDown(int keyCode, KeyEvent event) {
  4.  
  5.            if(keyCode == KeyEvent.KEYCODE_BACK){
  6.  
  7.                    data.putExtra("blah", "");
  8.  
  9.                    filebrowser.this.setResult(SUCCESS_RETURN_CODE,data);
  10.  
  11.                    filebrowser.this.finish();
  12.  
  13.                    super.onKeyDown(keyCode, event);
  14.  
  15.            }
  16.  
  17.            return true;
  18.  
  19.    }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Erykgecko
Junior Developer
Junior Developer
 
Posts: 20
Joined: Sat May 16, 2009 5:32 pm

Help. App crashes and dont understand why?

Postby luthepa1 » Wed Oct 28, 2009 8:46 am

Ok I'm a noob who cant understand why my app crashes with a "Force Close" exception. I basically am following this post for learning how to work with sub-activities and return data from that sub-activity. I can get it to work using a test method by just returning a string value in quotes. But when I add
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. EditText txtWorkoutName = (EditText)findViewById(R.id.newTxtName);
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

it will crash even though I dont even pass the txtWorkoutName variable back through the intent.

Here is what works

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class neweditworkout extends Activity {
  3.  
  4.     /** Called when the activity is first created. */
  5.  
  6.     @Override
  7.  
  8.     public void onCreate(Bundle savedInstanceState) {
  9.  
  10.         super.onCreate(savedInstanceState);
  11.  
  12.         //Inflate View
  13.  
  14.         setContentView(R.layout.neweditworkout);
  15.  
  16.         final Button btnSelect = (Button)findViewById(R.id.newButtonSelect);
  17.  
  18.         btnSelect.setOnClickListener(new View.OnClickListener() {      
  19.  
  20.                         //@Override
  21.  
  22.                         public void onClick(View v) {
  23.  
  24.                         //EditText txtWorkoutName = (EditText)findViewById(R.id.newTxtName);
  25.  
  26.  
  27.  
  28.                     Intent data = new Intent();
  29.  
  30.                     data.putExtra("result", "TEST_DATA");
  31.  
  32.                                 neweditworkout.this.setResult(RESULT_OK, data);
  33.  
  34.                                 neweditworkout.this.finish();
  35.  
  36.                         }
  37.  
  38.         });
  39.  
  40.     }
  41.  
  42. }
  43.  
  44.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


And here is what cause the application the crash when clicking the button
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class neweditworkout extends Activity {
  3.  
  4.     /** Called when the activity is first created. */
  5.  
  6.     @Override
  7.  
  8.     public void onCreate(Bundle savedInstanceState) {
  9.  
  10.         super.onCreate(savedInstanceState);
  11.  
  12.         //Inflate View
  13.  
  14.         setContentView(R.layout.neweditworkout);
  15.  
  16.         final Button btnSelect = (Button)findViewById(R.id.newButtonSelect);
  17.  
  18.         btnSelect.setOnClickListener(new View.OnClickListener() {      
  19.  
  20.                         //@Override
  21.  
  22.                         public void onClick(View v) {
  23.  
  24.                         EditText txtWorkoutName = (EditText)findViewById(R.id.newTxtName);
  25.  
  26.  
  27.  
  28.                     Intent data = new Intent();
  29.  
  30.                     data.putExtra("result", "TEST_DATA");
  31.  
  32.                                 neweditworkout.this.setResult(RESULT_OK, data);
  33.  
  34.                                 neweditworkout.this.finish();
  35.  
  36.                         }
  37.  
  38.                 });
  39.  
  40.     }
  41.  
  42. }
  43.  
  44.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


And here is the onActivityResult from my main activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.     // Listen for results from sub-activities.
  3.  
  4.     //@Override
  5.  
  6.         protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  7.  
  8.                 super.onActivityResult(requestCode, resultCode, data);
  9.  
  10.              Toast.makeText(this, "Returned: "+data.getStringExtra("result"), Toast.LENGTH_LONG).show();
  11.  
  12.         }
  13.  
  14.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


As I say, just uncommenting that one line causes the crashes.

Thanks in advance, Paul.
luthepa1
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 14, 2009 11:51 pm

Postby luthepa1 » Thu Oct 29, 2009 7:21 am

I found the problem. Please excuse my previous post. My findViewById was pointing to a TextView instead of a EditText view. All works now :D
luthepa1
Junior Developer
Junior Developer
 
Posts: 12
Joined: Wed Oct 14, 2009 11:51 pm

Top
PreviousNext

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests