Pass data to and return data from Sub-Activity

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

Pass data to and return data from Sub-Activity

Postby NickStan » Fri Dec 18, 2009 5:41 pm

I am busy writing an app that has a login screen and a stats page.

The login screen gets a username and password and passes it to the stats page.
I want to to call the stats page as a sub-activity and return 'Login failed' or show the stats (if login successful)

I tried to base my app on this tutorial http://www.anddev.org/tinytut_-_passing ... -t305.html done by plusminus, but when my app starts I get a force close error message.

Can anyone point me in the direction of sub-activity tutorial?

I will attach the code from my project

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.sub.activity">
  7.  
  8.     <application android:icon="@drawable/icon">
  9.  
  10.         <activity class=".MainActivity"
  11.  
  12.            android:name="MainActivity"
  13.  
  14.                 android:label="@string/main_app_name">
  15.  
  16.             <intent-filter>
  17.  
  18.                 <action android:value="android.intent.action.MAIN"
  19.  
  20.                                 android:name="MainActivity"/>
  21.  
  22.                 <category android:value="android.intent.category.LAUNCHER"
  23.  
  24.                                 android:name="MainActivity"/>
  25.  
  26.             </intent-filter>
  27.  
  28.         </activity>
  29.  
  30.         <activity class=".subActivity"
  31.  
  32.                 android:name="subActivity"
  33.  
  34.                 android:label="@string/sub_app_name">
  35.  
  36.             <intent-filter>
  37.  
  38.                 <action android:value="android.intent.action.VIEW"
  39.  
  40.                                 android:name="subActivity"/>
  41.  
  42.                 <category android:value="android.intent.category.NORMAL"
  43.  
  44.                                 android:name="subActivity"/>
  45.  
  46.             </intent-filter>
  47.  
  48.         </activity>
  49.  
  50.     </application>
  51.  
  52. </manifest>
  53.  
  54.  
Parsed in 0.004 seconds, using GeSHi 1.0.8.4


main.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.         xmlns:android="http://schemas.android.com/apk/res/android"
  7.  
  8.    android:orientation="vertical"
  9.  
  10.    android:layout_width="fill_parent"
  11.  
  12.    android:layout_height="fill_parent"
  13.  
  14.    >    
  15.  
  16.      <Button android:id="@+id/mainactivity_cmd_opensub"
  17.  
  18.          android:layout_width="fill_parent"
  19.  
  20.          android:layout_height="wrap_content"
  21.  
  22.          android:text="Launch Sub Activity" />                    
  23.  
  24. </LinearLayout>
  25.  
  26.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


subactivity.xml
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.    android:orientation="vertical"
  7.  
  8.    android:layout_width="fill_parent"
  9.  
  10.    android:layout_height="fill_parent"
  11.  
  12.    >
  13.  
  14.     <EditText android:id="@+id/subactivity_edit_returnvalue"
  15.  
  16.         android:layout_width="fill_parent"
  17.  
  18.         android:layout_height="wrap_content"
  19.  
  20.         android:background="@android:drawable/editbox_background"
  21.  
  22.         android:singleLine="true"
  23.  
  24.    />
  25.  
  26.      <Button android:id="@+id/subactivity_cmd_return"
  27.  
  28.         android:layout_width="fill_parent"
  29.  
  30.         android:layout_height="wrap_content"
  31.  
  32.         android:text="@string/subactivity_cmd_return"
  33.  
  34.    />
  35.  
  36. </LinearLayout>
  37.  
  38.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


MainActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.sub.activity;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.Intent;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.View;
  13.  
  14. import android.view.View.OnClickListener;
  15.  
  16. import android.widget.Button;
  17.  
  18.  
  19.  
  20. public class MainActivity extends Activity {
  21.  
  22.     protected static final int SUB_ACTIVITY_REQUEST_CODE = 1337;
  23.  
  24.  
  25.  
  26.      /** Called when the activity is first created. */
  27.  
  28.     @Override
  29.  
  30.     public void onCreate(Bundle icicle)
  31.  
  32.     {
  33.  
  34.         super.onCreate(icicle);
  35.  
  36.                
  37.  
  38.         // Apply our main.xml-Layout
  39.  
  40.         setContentView(R.layout.main);
  41.  
  42.    
  43.  
  44.         // Find the button defined in the main.xml
  45.  
  46.         Button cmd_opensub = (Button)findViewById(R.id.mainactivity_cmd_opensub);
  47.  
  48.        
  49.  
  50.         // Add an OnClickListener to it, that will open the SubActivity
  51.  
  52.         cmd_opensub.setOnClickListener(new OnClickListener(){
  53.  
  54.                // @Override
  55.  
  56.                public void onClick(View arg0) {
  57.  
  58.                    
  59.  
  60.                     //Intent i = new Intent(MainActivity.this, subActivity.class);
  61.  
  62.                    
  63.  
  64.                     // We use SUB_ACTIVITY_REQUEST_CODE as an 'identifier'
  65.  
  66.                     //startSubActivity(i, SUB_ACTIVITY_REQUEST_CODE);
  67.  
  68.                    
  69.  
  70.                     //startsubActivityForResult(intent, sub_activity_request_code);
  71.  
  72.                  
  73.  
  74.                    //Intent i = new Intent(MainActivity.this, subActivity.class);
  75.  
  76.                    //startActivityForResult(i, 1234);                    
  77.  
  78.                                        
  79.  
  80.                }
  81.  
  82.         });        
  83.  
  84.     }
  85.  
  86.          
  87.  
  88.         }      
  89.  
  90.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


subActivity.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.sub.activity;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.content.Intent;
  9.  
  10. import android.os.Bundle;
  11.  
  12. import android.view.View;
  13.  
  14. import android.view.View.OnClickListener;
  15.  
  16. import android.widget.Button;
  17.  
  18. import android.widget.EditText;
  19.  
  20.  
  21.  
  22. public class subActivity extends Activity{
  23.  
  24.      
  25.  
  26.      protected final int SUCCESS_RETURN_CODE = 1;
  27.  
  28.      
  29.  
  30.     /** Called when the activity is first created. */
  31.  
  32.     @Override
  33.  
  34.     public void onCreate(Bundle icicle) {
  35.  
  36.         super.onCreate(icicle);
  37.  
  38.  
  39.  
  40.         // Apply our subactivity.xml-Layout
  41.  
  42.         setContentView(R.layout.subactivity);
  43.  
  44.        
  45.  
  46.         // Find the button defined in the subactivity.xml
  47.  
  48.         Button cmd_return = (Button)findViewById(R.id.subactivity_cmd_return);
  49.  
  50.        
  51.  
  52.         cmd_return.setOnClickListener(new OnClickListener(){
  53.  
  54.                // @Override
  55.  
  56.                public void onClick(View arg0) {
  57.  
  58.                     // Find the edittext defined in the subactivity.xml
  59.  
  60.                     EditText edit_returvalue = (EditText)findViewById(R.id.subactivity_edit_returnvalue);
  61.  
  62.                    
  63.  
  64.                     Intent data = new Intent();
  65.  
  66.                     data.putExtra("result", edit_returvalue.getText().toString());
  67.  
  68.                     subActivity.this.setResult(SUCCESS_RETURN_CODE, data);
  69.  
  70.                     subActivity.this.finish();
  71.  
  72.                    
  73.  
  74.                }
  75.  
  76.         });
  77.  
  78.  
  79.  
  80.        
  81.  
  82.     }
  83.  
  84.  
  85.  
  86. }
  87.  
  88.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4


Thanks

Nick
Dont resist the inevitable
NickStan
Freshman
Freshman
 
Posts: 7
Joined: Mon Oct 12, 2009 4:54 pm

Top

Postby achie1266 » Fri Dec 18, 2009 6:16 pm

As far as I know SubActivity has been deprecated.
You have to use startActivity or startActivityForResult.
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby NickStan » Fri Dec 18, 2009 6:25 pm

Hi Achie

Thanks for the info,

As you can see I have tried to call startActivityForResult in my app, but I have commented it out because I get the error message:

The method startsubActivityForResult(Intent, int) is undefined for the type new View.OnClickListener()

So there may be something that I missed in my app (but what?)

Thanks
Dont resist the inevitable
NickStan
Freshman
Freshman
 
Posts: 7
Joined: Mon Oct 12, 2009 4:54 pm

Postby achie1266 » Fri Dec 18, 2009 6:35 pm

The reason you got that error is because it is defined on Activity class and not on OnClickListener

so if you want to call it from the OnClickListener which is inside your MainActivity which extends Activity, you need to call it in this way inside the onClick method of your OnCLickListener
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MainActivity.this.startActivityForResult(i,j);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4

You may also look at the notepad Tutorial which also explains how to use startActivityForResult
http://developer.android.com/guide/tuto ... d-ex2.html
achie
User avatar
achie1266
Master Developer
Master Developer
 
Posts: 223
Joined: Mon Nov 09, 2009 10:56 pm
Location: Denver

Postby NickStan » Mon Jan 25, 2010 6:21 pm

Hi Achie

I had a look at the Notepad tutorial and (correct me if I'm wrong) it seems to pass data between activities using a SQLLite Database.

Now I need to pass a username and password from one activity to another so wouldnt storing it in a database be a potential security risk?

Or is there and encrypted way to do this?

Thanks

Nick
Dont resist the inevitable
NickStan
Freshman
Freshman
 
Posts: 7
Joined: Mon Oct 12, 2009 4:54 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests