andbook!.pdf - Learning Android Get an anddev.org - Android-Shirt Back to index
anddev.org Header Logo
FAQ Search Top rated articles Browse Feeds anddev.org - Authors Contact Details Register Log in

SubActivites with return value - The InputBox

Goto page Previous  1, 2, 3  Next
 
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials
Author Message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Mon Feb 04, 2008 12:35 pm    Post subject: Reply with quote

Hello venkat,

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

If I have some final values I prefer to store them in a Interface. Sample from AndNav:
Java:
public interface Constants {
     /** Overall DebugTag for whole AndNav. */
     public final String DEBUGTAG = "ANDNAV_DEBUGTAG";
     
     /** Shall be used if only one SubActivity is closed.*/
     public final int SUBACTIVITY_RESULTCODE_UP_ONE_LEVEL = 0;
     /** Shall be used if a chain of SubActivities is to be closed.*/
     public final int SUBACTIVITY_RESULTCODE_CHAINCLOSE = SUBACTIVITY_RESULTCODE_UP_ONE_LEVEL + 1;
     /** Shall be used to go back one level in the dialog-process. */
     public final int SUBACTIVITY_RESULTCODE_SUCCESS = SUBACTIVITY_RESULTCODE_CHAINCLOSE + 1;
}


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
venkat
Senior Developer
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Mon Feb 04, 2008 12:47 pm    Post subject: Reply with quote

thanks for your replay plusminus,
actually i need to pass data "from activity to subacitivity" not "subactivity to activity" Smile

_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
plusminus
Site Admin
Site Admin


Joined: 14 Nov 2007
Posts: 2660
Location: College Park, MD

PostPosted: Mon Feb 04, 2008 12:54 pm    Post subject: Reply with quote

Hello venkat,

the Tutorial description is : "Passing data to SubActivities"

I extracted relevant lines for you:
MainActivity (perhaps on Button Click):
Java:
                   Intent i = new Intent(MainActivity.this,
                                             SubActivityWithResult.class);
                   
                    /* Create a bundle that will  
                     * hold will be passed to the
                     * SubActivityover the Intent */

                    Bundle b = new Bundle();
                    b.putString("DEFAULTTEXT", "Some Default blabla...");
                    i.putExtras(b);
                    // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
                    startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);

and with the SubActivity do:
Java:
  // ...
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // Apply our subactivity.xml-Layout
        setContentView(R.layout.subactivity);
       
        /* Retrieve the String from the Extra-Bundle
         * we started this SubActivity with. */

        Bundle b = this.getIntent().getExtras();
        String s = b.getString("DEFAULTTEXT");
        // Find the EditText defined in the subactivity.xml
        ((EditText)findViewById(R.id.subactivity_edit_returnvalue)).setText(s);

        // ...


Regards,
plusminus

_________________
Download my apps Idea
Please remember, that this board is give & take Smile


| Android Development Community / Tutorials
Back to top
View user's profile Send private message Send e-mail Visit poster's website
venkat
Senior Developer
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Mon Feb 04, 2008 1:09 pm    Post subject: Reply with quote

Thank you very much Plus minus Smile ..
i will try it now and let you know if i have any doubt.

_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
venkat
Senior Developer
Senior Developer


Joined: 27 Nov 2007
Posts: 152
Location: India

PostPosted: Mon Feb 04, 2008 1:26 pm    Post subject: Reply with quote

it works Smile

thank you very much.. Smile Wink

_________________
Regards,
Venkat.
Back to top
View user's profile Send private message
bavarol
Experienced Developer
Experienced Developer


Joined: 10 Dec 2007
Posts: 52

PostPosted: Thu May 29, 2008 10:51 am    Post subject: Reply with quote

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?

Java:

Intent intent = new Intent(InvokingActivity.this, InvokedActivity.class);
intent.putExtra("anObject",object) ;
startSubActivity(intent, 0);


Java:

Bundle bundle = this.getIntent().getExtras();
if ( bundle != null ) {
          Object object = bundle.get("anObject"); <-- Error
}

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


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

How can I pass objects between activities?

Cheers


P.S.:I have the SDK m3rc22a.
Back to top
View user's profile Send private message
feelingAndroid
Once Poster
Once Poster


Joined: 26 May 2008
Posts: 1

PostPosted: Wed Jul 02, 2008 4:23 am    Post subject: showAlert problem Reply with quote

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
Back to top
View user's profile Send private message
mrdanger
Freshman
Freshman


Joined: 17 Jun 2008
Posts: 9
Location: Hamburg, Germany

PostPosted: Thu Jul 03, 2008 6:16 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
darolla
Master Developer
Master Developer


Joined: 25 Sep 2008
Posts: 221
Location: Dortmund, Germany

PostPosted: Tue Dec 30, 2008 1:40 pm    Post subject: Reply with quote

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

here comes the clue Rolling Eyes

this way the subactivity is called:
Java:

Intent intent = new Intent();
intent.setClass(this, SubActivity.class);
startActivityForResult(intent, 1234);


this way the "result" is given back to the calling activity
Java:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);

     Log.d(TAG, "" + requestCode);
     Log.d(TAG, "" + resultCode);
     Log.d(TAG, "" + data.getStringExtra( "result" ) );
}   


this way the "result" is send in the subactivity
Java:

Button someExitButton = (Button)findViewById(R.id.someExitButton);
someExitButton.setOnClickListener(new OnClickListener(){
    public void onClick(View view) {
     Intent data = new Intent();
     data.putExtra( "result", theResultString );
     setResult( RESULT_OK, data );
     finish();
    }
});                      


greetings,
darolla
Back to top
View user's profile Send private message
Artur79
Developer
Developer


Joined: 26 Sep 2008
Posts: 26

PostPosted: Thu Jan 15, 2009 12:57 pm    Post subject: Reply with quote

a line (adjusted to orginal code)

Java:

intent.setClass(this, SubActivityWithResult.class);


doesn't work
Back to top
View user's profile Send private message
ioRek
Freshman
Freshman


Joined: 22 Jun 2009
Posts: 9

PostPosted: Mon Jun 22, 2009 3:36 pm    Post subject: import Reply with quote

what's the import needed for showalert, still can't find it RHA
Back to top
View user's profile Send private message
xenon
Freshman
Freshman


Joined: 11 Jun 2009
Posts: 2

PostPosted: Thu Jun 25, 2009 2:09 pm    Post subject: Reply with quote

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

Java:

intent.setClass(this, SubActivityWithResult.class);


Error message

Quote:

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?
Back to top
View user's profile Send private message
Erykgecko
Junior Developer
Junior Developer


Joined: 16 May 2009
Posts: 19

PostPosted: Sun Jul 19, 2009 8:33 pm    Post subject: Reply with quote

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,

Java:
  @Override
   public boolean onKeyDown(int keyCode, KeyEvent event) {
           if(keyCode == KeyEvent.KEYCODE_BACK){
             data.putExtra("blah", "");
             filebrowser.this.setResult(SUCCESS_RETURN_CODE,data);
             filebrowser.this.finish();
                   super.onKeyDown(keyCode, event);
           }
           return true;
   }
Back to top
View user's profile Send private message
luthepa1
Junior Developer
Junior Developer


Joined: 14 Oct 2009
Posts: 11

PostPosted: Wed Oct 28, 2009 8:46 am    Post subject: Help. App crashes and dont understand why? Reply with quote

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
Java:
EditText txtWorkoutName = (EditText)findViewById(R.id.newTxtName);

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

Here is what works

Java:

public class neweditworkout extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Inflate View
        setContentView(R.layout.neweditworkout);
        final Button btnSelect = (Button)findViewById(R.id.newButtonSelect);
        btnSelect.setOnClickListener(new View.OnClickListener() {     
               //@Override
               public void onClick(View v) {
               //EditText txtWorkoutName = (EditText)findViewById(R.id.newTxtName);

                 Intent data = new Intent();
                 data.putExtra("result", "TEST_DATA");
                    neweditworkout.this.setResult(RESULT_OK, data);
                    neweditworkout.this.finish();
               }
     });
    }
}


And here is what cause the application the crash when clicking the button
Java:

public class neweditworkout extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Inflate View
        setContentView(R.layout.neweditworkout);
        final Button btnSelect = (Button)findViewById(R.id.newButtonSelect);
        btnSelect.setOnClickListener(new View.OnClickListener() {     
               //@Override
               public void onClick(View v) {
               EditText txtWorkoutName = (EditText)findViewById(R.id.newTxtName);

                 Intent data = new Intent();
                 data.putExtra("result", "TEST_DATA");
                    neweditworkout.this.setResult(RESULT_OK, data);
                    neweditworkout.this.finish();
               }
          });
    }
}


And here is the onActivityResult from my main activity
Java:

    // Listen for results from sub-activities.
    //@Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          Toast.makeText(this, "Returned: "+data.getStringExtra("result"), Toast.LENGTH_LONG).show();
     }


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

Thanks in advance, Paul.
Back to top
View user's profile Send private message
luthepa1
Junior Developer
Junior Developer


Joined: 14 Oct 2009
Posts: 11

PostPosted: Thu Oct 29, 2009 7:21 am    Post subject: Reply with quote

I found the problem. Please excuse my previous post. My findViewById was pointing to a TextView instead of a EditText view. All works now Very Happy
Back to top
View user's profile Send private message
Display posts from previous:   
       anddev.org - Android Development Community | Android Tutorials | Index -> Novice Tutorials All times are GMT + 1 Hour
Goto page Previous  1, 2, 3  Next
Page 2 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


© 2007, Android Development Community
All rights reserved.
Powered by phpBB.