Android - Access MainActivity Instance - android

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

Related

Unable to access the methods from one class to another class in android

Unable to access the methods from one class to another class in android.
Actually, what I am trying is
ClassA.java
class ClassA extends Activity
{
method_1();
}
ClassB.java
class ClassB extends BroadCastReciever
{
// I need to access the method_1 from the class ClassA.
}
How can I do this?
First Solution
Declare your method1() as follows -
public static void method_1()
{
}
Then you can access it from ClassB as follows -
ClassA.method1();
Second Solution
Create an object of ClassA and access method1() from ClassB as follows-
ClassA classA = new ClassA();
classA.method1();
EDIT:
If both the classes are in the same package then you can use protected instead of public in the First Solution.
inside ClassA declare public method from ClassB create object of ClassA and access method using object
write below code inside ClassB
Like
ClassA classa = new ClassA();
classa.method_1();
Note :
if possible don't create class as static if it is not necessary.
provide class modifier to public.
mark method_1(); as public and static
Eg:
class ClassA extends Activity
{
public void static method_1();
}
Then you should be able to access method_1(); without creating a object of class A.
i guess you cant access a method from a class that extends an activity in android , however you can add an inner or helper class to the activity class like this example :
Call a public method in the Activity class from another class?
there is two way to access it
1) you need to create method as public static method() so that you can access it anywhere by calling class name dot method name like Aaa.method();
2)or you need a object to call method like passing Aaa's object to broadcast class and call method on it.
EDIT:
if you are passing context from activity A to brodcast class then you can convert context into activity object to call its public methods.
like:
in brodcast class:
((yourActivityName)context).method();
Basically you can call any method from another class, as long as it's public. If the method is static, you don't even need to instantiate it. But read some tutorials to get the basic idea of OOP instead of just looking for a solution for your specific problem, it'll help a lot more!
You should never be able to to that ... How do you know if the activity is still alive? The best way to send messages from BroadcastReceivers to Activities (where you register a custom receiver in onCreate/onresume and unregister in onDestroy/onPause) is to send another broadcast message from BroadcastReceiver.
But you could catch the initial broadcast already in activity. Consider using OrderedBroadcastReceivers.

adding a method to android application

I am fairly new in android and Java and this question might seem a little bit a naive question ,
I am trying to write a method called getDispalay() , this will be a public with no retuen.
I will print a message saying I am in the getDisplay method .
In java it is very simple , It will be called in main method , But I do not know how i can use main method in activity class ? I mean how can I write a method and call it as I need it ?
Thanks
Go through this article
As you said in java we call the function in main method .
In android if java class extends activity you can call the method in Any of the life cycle event of activity , It depends on your need . For example if you want to call method when you enter into activity follow this
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)//To set up ui for activity
getDispalay();
}
public void getDispalay(){
//your actions
}
What you really need to do is understand the activity lifecycle:
Once you have read through that then you will know how your Android app runs and what states and stages it transitions through.
You should read the documentation about Activity Lifecycle.
The onCreate() method would be comparable to main().

Is it possible to call class which extends Application android?

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..

How to call one function in every activity of android project?

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..
}
}

How to get acces to findViewById from another class?

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.

Categories

Resources