How to call a main activity method from a sub activity

General topics about the Android-Platform itself.
Coding issues please to the subforum right below.

How to call a main activity method from a sub activity

Postby Katharnavas » Fri Feb 01, 2008 11:25 am

HI,
Can anyone explain me the possibility of calling a method of the main activity from a sub activity without creating a new instance of the main activity class..
Any suggestions please ..
Katharnavas
Senior Developer
Senior Developer
 
Posts: 100
Joined: Tue Dec 04, 2007 5:57 am
Location: India

Top

Postby plusminus » Fri Feb 01, 2008 4:50 pm

Hello Katharnavas,

why not 'outsourcing' the specific code to a class with static function (if its possible) :?:

Within Activity I could only find: .getCallingActivity() returning just a (here useless) ComponentName.

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 don » Sun Feb 03, 2008 3:30 pm

Not sure if this helps, but you could also move the method (plus any data it needs) to its own class that can be called by both. To get by the multiple instantiation, you could make the new class a singleton pattern.
don
Junior Developer
Junior Developer
 
Posts: 18
Joined: Fri Dec 07, 2007 5:17 am

Some basic questions on Activity to Activity interactions...

Postby tv_sathish » Sun Feb 03, 2008 6:35 pm

Hi all,
I have the opposite of the problem posted in this forum. I have the Main activity calling a SubActivity and I have a declared a member method in SubActivity class that I want to call in main activity. The member method depends on some attributes of the SubActivity class that can be initialzed using only other instance variables (like MapView object). So I cannot declare these attributes and the method I want to call as static? How then can I call the SubActivity method? How to do this especially after you call startSubActivity to create the subActivity? In the context of this method, which is the child Activity in Android terms?

The other question I have is whether the onCreate method of an Activity is equivalent to a constructor? Are you not supposed to use this.<> inside onCreate()? It throwed some exception when I used that line, and that exception went off when I commented the line this.startSubActivity inside onCreate of main Activity. That's why I wish to know if the onCreate method of an Activity is equivalent to a constructor?

The third question is whether any subActivity you call in main Activity also needs to be mentioned in AndroidManifest.xml? How can then we make the subactivity not appear in the list of activities in Launch configuration?

Thanks and regards,
Sathish
tv_sathish
Developer
Developer
 
Posts: 29
Joined: Wed Jan 09, 2008 12:32 pm

Postby plusminus » Sun Feb 03, 2008 7:38 pm

Hello tv_satish,

1.) Sorry no answer that would help you really further + no time to digg deeper. Never had to do that :(

2.) Yes there are some things which cannot yet be called within onCreate(), because it is a part of the Activity-initialization process. You could use a Handler with .postDelayed() to almost instantly after Activity is finished call a SubActivity. Did that to realize a simple splashscreen:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void onCreate(Bundle icicle) {
  2.                 super.onCreate(icicle);
  3.                 setContentView(R.layout.splashscreen);
  4.                
  5.                 /* New Handler to start the Menu-Activity
  6.                  * and close this Splash-Screen after some seconds.*/
  7.                 new Handler().postDelayed(new Runnable(){
  8.                         @Override
  9.                         public void run() {
  10.                                 /* Create an Intent that will start the Menu-Activity. */
  11.                                 Intent mainIntent = new Intent(Splash.this,Menu.class);
  12.                                 Splash.this.startActivity(mainIntent);
  13.                                 Splash.this.finish();
  14.                         }
  15.                 }, SPLASH_DISPLAY_LENGHT);
  16.         }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


3.) Only Activities with intent-filters in AndroidManifest.xml like this :darrow: are listed to the enduser in 'Applications':
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.             <intent-filter>
  2.                 <action android:value="android.intent.action.MAIN" />
  3.                 <category android:value="android.intent.category.LAUNCHER" />                
  4.             </intent-filter>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4

If you want it not to appear there, i.e. for all your SubActivities use the following intent-filter:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.             <intent-filter>
  2.                 <action android:value="android.intent.action.DEFAULT" />
  3.                 <category android:value="android.intent.category.VIEW" />
  4.             </intent-filter>
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Hope I answered (all) your questions sufficient.

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

How to hide subactivity in Launch configuration?

Postby tv_sathish » Mon Feb 04, 2008 5:39 pm

Hi Plusminus,
I tried using the Intent filter the way you mentioned, but it is still showing the subactivity in the Launxh configuration. See the AndroidManifest.xml below:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    package="com.google.android.Project">
  6.  
  7.     <application android:icon="@drawable/icon">
  8.  
  9.         <activity class=".Main" android:label="@string/app_name">
  10.  
  11.             <intent-filter>
  12.  
  13.                 <action android:value="android.intent.action.MAIN" />
  14.  
  15.                 <category android:value="android.intent.category.LAUNCHER" />
  16.  
  17.             </intent-filter>
  18.  
  19.         </activity>
  20.  
  21.         <activity class=".Sub" android:label="@string/app_name">
  22.  
  23.             <intent-filter>
  24.  
  25.                 <action android:value="android.intent.action.DEFAULT" />
  26.  
  27.                 <category android:value="android.intent.category.VIEW" />
  28.  
  29.             </intent-filter>
  30.  
  31.         </activity>        
  32.  
  33.     </application>
  34.  
  35. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Here "Sub" is the Subactivity that I do not want to appear in the list. Can you help me?

How to call a Subactivity automatically as soon as your main activity starts? Is it not possible to do this in onCreate? Can you explain a bit? What is the alternative then? And even if I could create a SubActivity from onCreate, how do I get the reference to that subActivity in main activity? OK, if it is not possible in onCreate, how is it possible in general? I can see that in FriendFinder.java you have a line that starts the FriendFinderMap subActivity, now after that call, is it possible for you to get the SubActivity reference in any way?

Finally, do you know how a Spinner can be monitored for selections to act accordingly? Both OnClickListener and OnItemClickListener seem to be inapplicable for Spinner? I also could not find a method in SpinnerAdapter that could help me to get the current spinner list selection?

Thanks and regards,
Sathish
tv_sathish
Developer
Developer
 
Posts: 29
Joined: Wed Jan 09, 2008 12:32 pm

Top

Postby plusminus » Wed Feb 06, 2008 10:51 pm

in a hurry...........

The reason it still shows up is probably that the change was not correctly recognized by the system. ( On a fresh system there will probably be just the one which got "launcher" in the androidmanifest.xml)
I have it exactly the same way with my AndNav with ~7 subactivitites by now and it show only the one with launcher.

Starting (Sub)Activity almost instantly, like a splash-screen:
:arrow: :src: viewtopic.php?t=782

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

Return to General

Who is online

Users browsing this forum: Yahoo [Bot] and 6 guests