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
Related
I'm extending the Application class for my additional custom need. And I'm calling a method inside that. As the expected behaviour, it is getting invoked for type of Android components(Activity, Service, Broadcast receiver, etc.,) But I want that too be invoked only on Activity. Is that any other way to overcome this problem ?
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
// the below method needs to invoked only for service
// but now called for all application components
myCustomMethod();
}
....
}
But I want that too be invoked only on Activity.
Can't be done. The Application instance will run if one component of your Application is open.
You need to do the customized stuff in another class and open it just when the instance of your desire component is open.
just add your code to onCreate method of your entry-point activity. If you want it to be called once per session - add two int keys to your shared preferences - app_launch_count and method_invoke_count. Increment first on App's onCreate and check the second in your Activity's onCreate if first greater then invoke the method :)
Move myCustomMethod() into the activity. An Application has no way of knowing what triggered the creation of its process.
Or, use registerActivityLifecycleCallbacks() on Application to register an ActivityLifecycleCallbacks object, and put your myCustomMethod() logic in onActivityCreated(). This requires a minSdkVersion of 14 or higher. That will tell you when each activity is created after your process is instantiated — if you only care about the first one, you would have to unregister the callbacks in your onActivityCreated() implementation.
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:
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);
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'm struck with some problem will calling the method from one activity to other......
I have a activity called Transaction2 in this activity,I have a method like getProposal().I need to call this method in to the other activity called PaymentDetails. I called the method like this : I import the activity(import com.Transaction2;) in to the paymentDetails and i create an object as
Transaction2 ts2 = new Transaction2();
and i call the method as ts2.getProposal();
when i called like this I am getting the exception like
java.lang.IllegalStateException: System services not available to Activities before onCreate()
Can anyone help me ?
Actually as i know, you cannot create a new activity by new. In android, Activity is suggest to designed as a standalone module, and the activity is created by android. If you want to communicate to other activities, you should use Intent to do this. And i strongly recommend you to put your application data out of your activity. Activity only take care of UI stuff.