I've imported google drive quick start project and it's MainActivity contains these functions:
com.google.android.gms.drive.sample.quickstart.MainActivity.onActivityResult(int, int, Intent)
com.google.android.gms.drive.sample.quickstart.MainActivity.onConnected(Bundle)
com.google.android.gms.drive.sample.quickstart.MainActivity.onConnectionFailed(ConnectionResult)
com.google.android.gms.drive.sample.quickstart.MainActivity.onDisconnected()
com.google.android.gms.drive.sample.quickstart.MainActivity.onPause()
com.google.android.gms.drive.sample.quickstart.MainActivity.onResume()
How does it work without onCreate method?
First of all I think you mean Activity onCreate method, not Application onCreate as you mentioned in the title. It is not the same. com.google.android.gms.drive.sample.quickstart.MainActivity implements android Activity therefore it contains default implementation of onCreate().
Related
Hi currently I'm having an augmented reality application and I'm using the open source code of DroidAR.
My problem right now is that in the augmented reality screen the code extends Setup ( which in provided by DroidAR ) instead of extends Activity like what we normally do. Thus when I wanted to override the back button I could not use onBackPressed. Is there any other way that I could override it?
Not familiar with DroidAR, but according to the source code of Setup, it has a "target" Activity that you pass as a constructor param (and can retrieve by calling getActivity()).
I suppose you could just simply override onBackPressed() in the Activity you're passing to the constructor of Setup (and if you would like to call it explicitly, you could just invoke getActivity().onBackPressed()).
I am working on some legacy code for an android app, and in some activities when they want to finish the activity they write
finish()
and in other places they write
activity.this.finish()
what is the difference?
actvity.this is transparent in an Activity class, because it references to itself, so you can call a class method both using and not using it as in Java
There is a class in a library that is called from javascript (html5 app). It can't be an Activity extender.
The class has access to Activity and WebView objects.
Is it possible to get onResume, onPause and onDestroy activity events from this library class?
On API Level 14+, you can call registerActivityLifecycleCallbacks() on your Application instance to find out about all callbacks to all activities in your app.
Or, modify your Activity to call the class in the library on each of those callbacks.
Mouse right click Source --> Override/Implement Method you can find methods which you need it. But make sure add your library on your project
I have started to explore the android world just some days ago, and I am doing it via the book by Mario Zechner, "Beginning Android Games".
I might have a ton of questions about the platform and the few things I have seen so far but I know it's going to get better. All I want to ask at the moment is about activities: I saw the activity life cycle. I know that activities are something like screens. The thing I don't know is that whether I have to specify the onCreate(), onResume() etc. methods in every activity I code.
As far as I know onCreate() is compulsory and the other methods depends on how you use the activity
The entire lifetime of an activity happens between the first call to onCreate(Bundle) through to a single final call to onDestroy(). An activity will do all setup of "global" state in onCreate(), and release all remaining resources in onDestroy(). So onCreate(Bundle) should have be there in activity.
Use of onResume() depends upon your application requirement.
for more details go to http://developer.android.com/reference/android/app/Activity.html
Welcome to the world of Android.
In general, it is good practice to flesh out all the methods such as onPause(), onResume() but when you create an android program, generally you only need to flesh out the onCreate() method for activities.
Besides the onCreate, and pardon if my terminology is incorrect, the other methods follow a "default" behavior if you do not override them. So if you need the application to do something specific when it is paused, that would be a good time to add your version of onPause(), otherwise you can leave it left out.
It is not mandatory that you have to specify all those methods or any of them. It depends on what type of implementation you want
Example
I have created my Activity (A) as it extends Activity I donot override any of methods like onCreate() but I have created some variables and created some of methods.
Let us assume I created second activity there I want to shoe some view I have used onCreate() method also If I want variables and methods which I have defined in activity A I can get those variables and methods If I write class B extends A
So it is not mandatory to use all those methods from activity. If you donot write your own implementation then the default implementation will come to play.
Short answer will be NO
You don't need to specify in code of each Activity onCreate and so on. Anyway in parent Activity there will be onCreate
But long answer says: good practice is not rely on implicit/invisible code, but to have code under your control (even if it's dummy). I used to code all onCreate/onDestroy, etc in this way:
public static final boolean DEBUG=true;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if(DEBUG)
Log.d(TAG, "Creating "+this.toString());
}
You should write onCreate() method by overriding it from the base class Activity to set the view. View should be generated here itself using setContentView() method in onCreate() method.
Regarding onResume(), onPause() and other methods, it is not mandatory to write these but are useful when you need to achieve specific functionality.
Also, being a beginner please have a look at the table 1 in this document: http://developer.android.com/guide/topics/fundamentals/activities.html
I have a class in my Android application that sub-classes the AndroidApplication object. The documents say:
public void onCreate ()
Since: API Level 1 Called when the
application is starting, before any
other application objects have been
created. Implementations should be as
quick as possible (for example using
lazy initialization of state) since
the time spent in this function
directly impacts the performance of
starting the first activity, service,
or receiver in a process. If you
override this method, be sure to
call super.onCreate().
I placed a breakpoint on my sub-class's constructor and when I run my application, it is never reached. Naturally, when I call the sub-class's getInstance() method from other code it returns NULL since the instance variable is (supposed to be) initialized when the constructor is called.
Can anyone tell me what is wrong? I would assume from the docs that I don't have to create an instance of the AndroidApplication sub-class myself, or do I? Am I supposed to modify my manifest file somehow to add the AndroidApplication sub-class and if so, how?
-- roschler
I'm posting the answer here for others. Yes you need to add the name of your Application object sub-class's name to the Android manifest. For Eclipse users, the easiest way to do this is to open the AndroidManifest.xml file, select the Application tab in the manifest editor, and use the Browse button next to the Name field to find your Android Application object sub-class name and select it. The manifest file will be updated properly to register it. I just did that and it worked.
I had a problem of not having a . before my application class name.
Should be:
android:name=".MyApp"
since the MyApp class is in the package defined in the manifest.