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

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);

Related

Stop asynctask from other activity

I creating an app using asynctask to download file from server. It has several activities. I start one asynctask from activity A and I want stop it from activity B. How can I archieve it ???
It is possible to call a previous activity's public methods by using typecasting of the getParentActivity() method on your activity B :
((PreviousActivity)getParentActivity()).somePublicMethod();
This only works if you opened Activity B from Activity A. If you want to be able to call that method from all activities, try creating a static class and save an instance of Activity A. This way, wherever you are on the app, the method can be called. Be also wary of null values when doing this.
This is the sample of the static class.
public static class Constants{
public static ActivityA activityAInstance;
}
When you open ActivityA (onCreate method) save it's instance:
Constants.activityAInstance = ActivityA.this;
This part saves the instance of ActivityA to your static class. So whereEver you are on the app, you can access that instance and call it's public methods:
e.g. the app is on ActivityZ all you have to do is import that static class and call ActivityA instance:
Constants.activityAInstance.somePublicMethodToStopTheAsyncTask();
In my opinion , you can set a boolean flag in activity A ,and when you run the asynctask , you should check this flag ever time,if you want to stop ,you just change the flag , so you could change this flag in activity B to stop the asynctask.
you should try this.
I think you can use LocalBroadcastIntent to send a message from activity B to activity A. In method receiver of A, you can call Asynctask.cancel(true). And you should place if(isCanceled()){
break;
}
in your asynctask's loop to stop the task when you call Asynctask.cancel(true).

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

calling oncreate within constructor

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 .

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.

Access Activity in a static way

I have an activity which has a static method for updating a textfield. This way I can update this view from another activity.
But now I'm trying to get a Context variable in this static method which is not possible. I've tried declaring a Context variable and initialising it in onCreate ( context = getApplicationContext();)
But still I can't access context in this static method. How is this normally done?
edit: a little bit more information about my situation. I'm starting a countdowntimer in an activity(a) which updates another activity's(b) ``textfield every second. And it does this by accessing b's setTextField in a static way..
How is this normally done?
Accessing a TextView via a static method is not the best way to update the field from another activity. If you want to pass a value to the activity when it starts, you can send data via the intent (i.e. intent.getExtras). If you want to pass data back from a sub-activity, you can use startActivityForResult.
The way you are going is very strange. Why are you trying to change one activity content from another? May be you need to use startActivityForResult to strat a new activity and then return result from it and change views depending on it?
You might want to check some documentation on OO and using static functions. It is not considered a very good approach.
But as we are not talking about a better complete sollution: you can add a parameter with a context to the function, and just give it when you call the function :)
I would suggest the LocalBinder pattern to update the other Activity:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
Can you do something like this?
something like this <viewobj>.getContext()
Ref: How can I start an Activity from a non-Activity class?
Whenever you're busy with Activity A, there's no point in updating something on Activity B as it is simply not shown to the user at that point in time.
Seems to me you need to have some kind of global variable here that can be picked up in the onResume of Activity B.
Checkout this question : How to declare global variables in Android?
It shows you how to use the Application class to maintain global application state, accessable from all activities when needed.

Categories

Resources