onCreate() of Application not called everytime

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

onCreate() of Application not called everytime

Postby and4m » Wed Aug 12, 2009 7:07 am

Hi All,

I extended the Application in a class (MyApplication.java) and made it's entry in manifest.xml.
However the onCreate() of MyApplication is not called every time, it is called for the first time when I run it on emulator. When I again start the application by clicking the launcher icon on menu screen, onCreate() of Application is not called.
Can anyone please tell me how can we ensure that the onCreate() of the application class is called every time and at the beginning?

Waiting for reply :idea:
Thanks,
and4m
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 07, 2009 5:58 am

Top

Postby arams » Wed Aug 12, 2009 11:33 am

post your code
arams
Experienced Developer
Experienced Developer
 
Posts: 89
Joined: Fri May 08, 2009 7:45 am

Postby padde » Wed Aug 12, 2009 1:23 pm

close your activity with finish()... and please read the activity livecycle you can
find HERE
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Emmanuel7 » Wed Aug 12, 2009 8:30 pm

Have a look at the link provided by padde...

OnCreate is only called on ... creation :)

Chances are that in your case, your application is still running. So when you call it back by clicking the icon, you just bring back to the foreground. And OnCreate is not called, but OnResume or OnStart.


Hope it helps
Emmanuel7
Senior Developer
Senior Developer
 
Posts: 164
Joined: Fri Dec 12, 2008 2:17 am
Location: Paris

Postby freewheelnat » Thu Aug 13, 2009 5:12 pm

If you want the app to finish when people hit the back button, then you can do something like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public boolean onKeyDown(int keyCode, KeyEvent event) {
  2.  
  3.                    
  4.  
  5.         switch (keyCode) {
  6.  
  7.                
  8.  
  9.         case KeyEvent.KEYCODE_BACK:    
  10.  
  11.              finish();
  12.  
  13.              return true;
  14.  
  15.        }
  16.  
  17.          
  18.  
  19.         return false;
  20.  
  21.    }
  22.  
  23.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


But you might not want this behaviour, it's up to you really.
freewheelnat
Developer
Developer
 
Posts: 40
Joined: Sat Aug 08, 2009 11:48 am
Location: London

Postby and4m » Fri Aug 14, 2009 6:05 am

Thanks for reply,

But I guess I couldn't post my question properly, I'll try again.

I am talking about android.app.Application and not about android.app.Activity.
There is no such method as finish()/ onResume() in Application class.
There are only four methods in it and last callback method that is called is onTerminate() but it not guaranteed to be called every time. I tried putting a Log in onTerminate() but it is not called a single time.

As far my understanding if we exit the application and then start it by clicking icon, the Application/MyApplication class's onCreate() should be called. Please correct me if I'm wrong.

Here is code that I tried,

Code: Select all
public class MyApplication extends Application {

   @Override
   public void onCreate() {
      
      super.onCreate();
      System.out.println("On create of MyApplication is caled !!!!!!");
      // start timer
      System.out.println("On create of MyApplication is ending !!!!!!");
      
   }
   
   @Override
   public void onTerminate() {
      
      super.onTerminate();
      // stop timer
      
   }
   
   
}


and the xml entry of it is,
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <application android:icon="@drawable/icon" android:label="@string/app_name" android:name="my.pack.MyApplication">
Parsed in 0.001 seconds, using GeSHi 1.0.8.4


Regards,
and4m
Freshman
Freshman
 
Posts: 6
Joined: Fri Aug 07, 2009 5:58 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests