We all know you can't use static on findViewById so ...
The basic code is:
public class DiffViewFlowExample extends Activity implements OnItemClickListener {
#Override
public void onCreate() {
}
static void hereismymethod() {
}
How can I use a findViewById here? I know I can't locally but because static won't work then...
Oh you might say: add it as an argument,well I would,but I will call the hereismymethod from a service,and we all know that services don't like to play with stuff that is about display...
So can anybody save me?
You shouldn't be calling method2, static or not, on an Activity from outside of the Activity. If you want to act upon your Activity from within the Service, then create a listener interface and then register your activity as a listener inside of the service. Then, when it's appropriate, have your service find the view and act on it. To allow the service to access the views in your Activity, simply make your interface method pass a reference to the Activity. Then, the Service can call activity.findViewById() and do whatever it wants to it.
Related
I have a fragment which extends DialogFragment and which need to notify other objects when its content changes.
The android fragment guide, state that the best way to do this is to require that the containing activity implements an specified interface and then cast the activity to that interface and call the method.
This does work, but it has the limitation that only the containing activity can listen for changes. What do I do if I want to be able to notify arbitrary objects?
I can't just take make a method which take a listener as argument, because the fragment may be re-constructed by Android behind my back. And I can't put the listener in a bundle, because I have no way of knowing if the class implement the listener can be persisted/serialized and I really don't all listener objects to be duplicated.
My fragment is used both in a popup, and as a normal fragment which is shown in the main view
You should try the EventBus library. Basically you can register to an event from anywhere and send events from anywhere. Events are plain Pojos which can contain any variables or other objects.
This makes the whole communication between Activities, Fragments and so on so much easier.
An alternate event bus solution is to use Square's Otto
Or you can try the LocalBroadcastManager
Make and interface, which suports callbacks, for example:
interface IListener{
public void onEventRaised(object arguments);
}
Then make a static class which has a List of listeners and calls them when you raise the event, for example:
public static class AppListenerDispatcher{
private static List<IListener> listeners; //make add and remove methods
public static void raiseMyEvent(object arguments){
for (IListener listener: listeners)
listener.onEventRaised(arguments);
}
}
And now you just need to implement this interface in the coresponding class that will listen for this event and add it to the list. Then you can call the AppListenerDispatcher.raiseMyEvent(arguments) anywhere and all the listeners in the list will do their corresponding work.
i have a public method in my MainActivity.java and want to call it from another Activity. Therefore i need the MainAcitivity Instance. How can i access it from another Acitivity.
Thanks
You can get help about message passing between activities by Broadcast Receivers
As already mentioned in comments that "This isn't something that should normally be required", but still if you want this you can make that method static in MainActivity.java.
public static void someMethod()
{
}
and then in another activity, call this way:
MainActivity.someMethod();
I have a class which extends Application and i want to call it from code, it have
#Override
public void onCreate()
I need to call this from an Activity. I know how to call it when app starts for that i need to include in manifest:
android:name=""
Thanks.
You should avoid Calling Applications onCreate manually, as it will get started automatically if anything is configured correctly. However, If you want to call Methods from your overridden Application you can do it like this:
public class MyApplication extends Application{
public void someMethod(){}
}
then inside any Activity:
MyApplication app = (MyApplication)getApplication()
app.someMethod();
Try this :
public class YourApplication extends Application
{
public void sayHello {
System.out.println("Hello")
}
}
Then call it in any activity by:
YourApplication appState = ((YourApplication)this.getApplication());
appState.sayHello();
Application class onCreate() gets called when the Application starts. If you want to call a method that you have declared in your Application class you can call it like,
((Application_Class_Name)getApplicationContext()).calling_method();
From any other class that extends Activity, else you have to use context to get the instance of getApplicationContext() to call from Non Activity class.
Eg - If you want to call it from Adapter class you need to pass the context of the Activity to adapter class and get the instance of Application,
((Application_Class_Name)mContext.getApplicationContext()).calling_method();
From Activity you simply call ((YourAppName)getApplicationContext()).
And also, you don't need onCreate() in your Application (unless you know you do). You can set some methods there and then call them with e.g. ((YourAppName)getApplicationContext()).myMethod(). Your app is alive as long as any of its activities is.
You should't call onCreate() method by yourself... Android does it for you... The main purpose of such a class is to keep global variable commom to whole application, since Application itself is a single instance...
And it lets you override onCreate() because you may need your Custom things in an Appliucation/ACtivity that the OS does create..
I have a one notification service which fires at every 5 seconds and get the data and parse it and then display it..
But this mechanism works only for the home screen of the app.
I want this to be done in every screen of the app.For that should i write the same code in every activity of the project ? It is very tedious job . I want to make it generalize ..
How to do that ?? any ideas??
you can make BasicActivity and have method in that Activity and call that method inside onCreate();
now extend every Activity in your project from your BasicActivity
If you are talking push notification service, this idea seems to problematic. tell us more what are you exactly trying to do.
you do something like this in some utility class
public static void showNotifction(Activity a)
{
//notify
}
and over this if you have any call back methods.. Then create an interface with those methods and implement them in all those Activities and you can do this..
public static void showNotifction(CommoInterface a)
{
if(a insatncof Activity){
//notify
a.callBack(); // callBack is a method in the interface..
}
}
I have a set of commands that I want my app to run when it's restarted, regardless of what activity it's on. I know I need to put an onRestart() into every one.
Since it's the same set of commands regardless of what activity it's on, is there a way I could have them all refer to a single function for that? It seems like that would be better then having to copy paste the commands into each onRestart() handler. It will be a lot less work if I need to change the set of commands too.
You have a couple of options, depending on the code.
You can put it in a helper class as a static function: public static void doWork() { .. } This should work, unless whatever you are doing depends on being in the activity. You can generally just pass it what it needs though, like the Context.
Or, you could extend Activity with your own class, MyActivity and place the work in that onResume. Then extend MyActivity for each of your real activities. They will now automatically do that work when you call super.onResume(). This works well as long as you really want to do the same thing in every activity, and don't use a lot of specialized activities like ListActivity.
Edit:
public class MyHelper {
public static void doWork() {
// do your work here
}
}
public class MyActivity extends Activity {
public void onResume() {
super.onResume();
MyHelper.doWork();
}
}
A search for "static method" will provide more details.
Derive all your activities from a single class (something like ActivityBase) that, in turn derives from system-provided Activity. Then implement onRestart() in ActivityBase.
Is it your application that is restarting from scratch? Or just your activities that are restarting/resuming?
The Application class has an onCreate() method, and you can extend Application in your app to override its behavior. Just remember to change the name of the application in AndroidManifest.xml so it picks up your custom Application class when starting. This code would run before any activities start up. But it won't run every time an activity is stopped and restarted. If that's what you need, this won't do it.
You could also implement a singleton class that contains an initialize() method, or restart() method. You simply call it from onRestart() in each activity you want it in. It sounds like this special code ought to be localized away from your activities so I don't think I'd recommend extending Activity to put the code there.