I am new to android.I need some clarification about Activity and Activity life Cycle.
My Questions are:
1.Who is going to create an object for Activity.
2.Who is calling all life cycle methods of android.Please can any one clarify it.
Thanking in Advance.
You cannot just create objects of Activities by using:
MyActivity activity = new MyActivity();
Android itself call at runtime both activity and its lifecycle..
as you would with normal Java classes. All Activities in Android must go through the Activity lifecycle so that they have a valid context attached to them.
By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.
Instead, move all such methods which need to be called from other classes into a Utility class which accepts a valid context in its constructor, and then use that context in the methods to do the work.
LifeCycle of Activity:
Related
I have a class in my app that extends the Application class. I have declared/named it in the Manifest.xml file. It basically initializes some stuff but more importantly gives me access to a serial port on the machine I'm programming on. Here is my question. I have an activity where I subclass this Application class. The Activity, lets call it Activity A initializes a variable that is declared in it's superclass (the one that subclasses Activity).
When I create an intent in Activity A meant to send the user to a different Activity, after calling startActivity(intent) I call finish(). Will that completely erase Activity A from memory, or, because in Activity A I reference a variable declared in the Application class, will this Activity A remain in memory
Nope, actually when we use superclass properties in sub class it won't be more in memory. So when ever we call for finish() for activity. All declared variables and all references related to that Activity will be destroyed (removed from memory).
I'm badly confused and hope to get your help understanding this concept.
I have an app with 3 activities, splash, login and main and the main activity is a multi fragment drawer activity that uses sqlite.
In my fragments i need to use context many places. I used to have a static context in my main activity defined and passed that around but in another questions someone suggested not to use static context to avoid leaks and i took the advice.
I had to change a few things and got things working. I use getapplicationcontext() but now my application now consistently crashes. The crashes are more prominent when the app is put in background.
My question is, which activity's context should i be using to start with? Splash? Login or main? How would you get access to the context in resume?
Thanks
If you are in A fragment you can use getActivity() to access its container activity context.
If you are try to access activity context from an adapter class or dialog, you must pass the activity context to the constructor of the adapter class or dialog
If you are in an activity you can use this or yourActivity.this as per the situation.
For example
1)If you want to access activity context from onResume() or onCreate() you can simply use this.
2)If you are try to access activity context from an inner class like retrofit call then you must use yourActivity.this for activiy context
I am new to android development.
So I have a question about activities.
On Google developer, they say you have to make a class which is inheriting from the class Activity.
So far so good. Now I am thinking an Activity is a class, which leads to my major problem.
So when activity is a class how can it invoke all the live cycle methods.
In Java, I learnt there must be an object of a class to invoke methods and actions. So when activity is not an object but a class, how is this possible.
Kindest regards
Jan
When a user starts your app the android operating system will instantiate an object based on your class which must be a subclass of Activity.
The operating system will use that object and call the lifecycle hooks (e.g. onCreate, onResume, ...) which you implented in your activity subclass when one of the related lifecycle events happens.
Here it is clearly mentioned under CREATING AN ACTIVITY header, that system calls the callback methods, based on which event has occurred. Suppose user is interacting with your activity, then using the object created by the OS for that activity, all the callback methods will be executed. Please refer.
My launch activity starts up another activity whose launch is set to single instance. In this 2nd activity, I have a public method. I then start up a 3rd activity and that activity needs to access the public method in the 2nd activity. I don't want to use startActivity and pass it extras because I assume the onCreate will get called (or am I wrong?) and I need to avoid the 2nd activity from reinitializing itself.
When an activity is started using startActivity, is it possible to gain access to the underlying class instance itself and simply call the method?
I actually came up with a simple solution. As a matter of fact you can access the underlying class of an activity. First, you create a class that is used to hold a public static reference to activity 2. When activity 2 is created, in its onCreate method you store "this" in the static reference. Activity 2 implements an interface with the methods that you want available to any other activity or object. The static reference you hold would be of a data type of this interface. When another activity wants to call a method in this activity, it simply accesses the public static reference and calls the method. This is no hack but is intrinsic to how Java operates and is totally legitimate.
It is not a good idea.
As I can understand method from second activity is actually not connected to particular activity while you want to call it from another one. So carry the method out to other (non-activity) class (maybe static method) and use it from both activities.
It's not directly possible to gain access to activity object started using startActivity (without using some hacks). And frankly you shouldn't even trying to accomplish this.
One Activity component can cycle through several Activity java object while its alive. For example, when user rotates the screen, old object is discarded and new activity object is created. But this is still one Activity component.
From my experience, when you need to do things you described, there is something wrong with your architecture. You either should move part of activity's responsibilities to Service or to ContentProvider, or use Intents, etc. Its hard to recommend anything more specific without knowing more details.
No there is no way to pass a reference via startActivity() however you can use some sort of shared memory to keep reference to your Activity. This is probably a bad design. However passing an extra with your Intent will not cause onCreate, that is completely related to the lifecycle.
I have a helper class that I need context so I can access the SharedPrefences. Other posts recommend passing in the application context on instantiation of the helper class. So I made that change, it works very well except within a tab activity. The tab activity need to call a webservice to determine what data to display. The helper class makes the webservice call.
You can call getContext() from any activity. If the helper class is defined as a subclass of the activity, it can call it directly. Otherwise, passing the context through instantiation would be my second choice. I agree, it's not pretty passing contexts everywhere. There are probably some complicated OOP patterns you could use to avoid this, but I can't see it being an advantage overall.
If you get a null pointer you might be calling the function too early. In what function are you calling it?