calling oncreate within constructor - android

I am calling a class's member function from another class. So,I am creating an object of the class for which i have to call constructor. Within that constructor, I need to call onc reate method of my activity class.how can I do it?

Do not call onCreate from your constructor. The system itself will call onCreate for you.
Take a look at the Activity Lifecycle for more information of how onCreate is called.

Well,i presume u r trying to call a constructor in an activity.If that is the case then some thing is wrong in the way u have designed ur project.For more details check out these links
Creating an object of Android Activity class and Android - Activity Constructor vs onCreate

All method's of Activity class should be called within activity only . write your own methods for whatever operation you need to perform and pass data from other Activity/class by various data transfer algorithms .

Related

Who will be create on object for Activity?

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:

Pass Variables from AsyncTask back to calling to Activity

I am new to Android Development and have a question (will probably show my newbie status). I am calling an asynchtask from a custom activity. Once the asynch task is completed onPostExecute I would like to call back into my activity and set a pojo (Map()). I know that onPostExecute seems to run on the UI thread but I am not sure how to get visibility into the calling Activity.
The goal is to be able to have some variables set in my activity and ideally the webservice call will already be completed.
Thanks,
Craig
If the AsyncTask is a subclass of the activity, it has access to all public, protected, and private variables of the superclass. If its not, you need to write a public function in the Activity class to set the variables, and call it from the AsyncTask. That will probably require you to pass the activity to the AsyncTask via the constructor and save it in a member variable of the task.

Object Creation in Android Componets

I am trying to make myself clearer in android component instance creation.
I believe any activity itself is an instance (correct me if i am wrong) but what would actually call " new " for object/instance creation? how does this whole stuff works in android framework... do we use super() in each component for that ? (to create that instance) which calls the base class's (Activity) constructor and it eventually calls "new" and creates the new instance for its derived class?
to start an activity... you should call this method inside your activity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);<--- here is the call for the activity(base) class..
without oncreate your activity will not have any instance..it will not start..
You don't directly create an Activity instance, instead you send an "Intent" to tell the system to start one for you.
For more information on how to implement, start, and manage Activity, see here

Call a member function from an activity, without starting that activity

In my application, there are two activity classes. Suppose A and B. Activity B has a member function name myfun(), Is it possible to call myfun of activity B from activity A with starting activity B. If yes, please provide solution
thanks
You can initiate a object of your ActivityB, and than run the member function. (Use it like an normal object)
E.g
ActivityB act = new ActivityB();
act.yourMethod();
(the onCreate will not run when using the "new" keyword, only the constructor of ActivityB.)
Btw. If the method is not depending on any special state or member variables of the ActivityB class, I sugges you move/refactor the method to another Util class or something like that.
you can create for sure, but you won't able to use that object to start activity, instead you can create a static method, or variable, and can use directly.
public static myFunction(int parameter)
{
// Your code
}
and call this method by
YOurActivtiyClass.myFunction(parameter);

Android: Access method in an activity from another activity

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.

Categories

Resources