How to call constructor from one page to other in android? - android

I have two classes, Class A and Class B. In Class B I created a constructor. In class A I created onresume method. From the onresume method of class A I want to call the class B constructor.
How can I do that? Please guide me regarding this.
Thanks in Advance

When you create a new object of class B in onResume of class A, it will automatically call the constructor for class B for its object.
For example:
B obj = new B(); //the 'new' keyword will call the constructor itself

If both classes are public and are of same package, you can call constructor of class A in class B by using the code,
B objb = new B();
Otherwise you need to extend class B from class A.

Related

How to pass arguments to superclass fragment

I know with Fragments we cannot use its constructor because the system needs the empty one. So to pass data we need to use a static method and bundles like
public static A newInstance(int myInt){
A myA = new A();
Bundle args = new Bundle();
args.putInt("myInt", someInt);
myA.setArguments(args);
return myA;
}
Okay so far so good.
But what if I want to generify this?
public abstract class C{
private int myInt;
//Here I cannot use the newInstance because I cannot create an instance of an abstract class
}
The outcome should be that I have constructs like this
public class A extends C{
//Some stuff that needs myInt which is stored in superclass because every subclass fragment needs it
}
public class B extends C{
//Some completly other stuff that also needs myInt
}
Both A and B need myInt so I would like to have a newInstance method provided by the superclass that I can call and the outcome should be a instance of A or B. How to do this/Is this possible?
In class C you can store the instance variable myInt, and then override onCreate() and in that extract the myInt value from the arguments bundle.
When creating a new A (A.getInstance(...)), you need to put the myInt value in the argument bundle. You need to do the same thing when creating a new B. To avoid copy-pasting code, you can add a helper method in C called (for example) createArgumentsBundle(myInt), that creates a new bundle with myInt in it and then send back the bundle.
It is not mandatory to have a static method in fragment class to create it's instance. You can create it from outside as well. A Parent class usually is not supposed to know about its child classes.
Retrieve the Bundle arguments from the base class C and set myInt there.
A and B will get the access. Make sure myInt has a protected access modifier.

How to update view in a method in called from another class?

In class A a have a method that updates a view.
First I called Class b to download some data, once the data is recieved I call the method in class A.
When the view is updated (when the method is called from class b) the view is throws NPE.
However if the view is updated (when called from its native class (class A)) it works perfectly.
Class A is an activity.
In some cases it reported there is some sort of context problem.
I have tried implementing an interface however even that threw an NPE
How can I fix this problem?
public class A extends activity {
public() {
}
B b = new B();
b.doSomething();
public void myMethod(String string) {
textView.setText(string)
}
}
public class B {
public void doSomething() {
String data = getData;
A a = new A();
a.myMethod(data)
}
}
You should not call new A(). This will not invoke a new Activity with the application lifecycle, and will not provide you a reference to the original Activity that contains the View you are trying to update.
I would recommend A should implement a simple interface YourInterface that contains a void callbackMethod(String data);.
Pass your object A (as this) into B, such as on doSomething(this), changing the signature of doSomething to doSomething(YourInterface callback).
In B you may then call callback.callbackMethod(data), processing data within your Activity A.

Static method of singleton class used in multiple activities

There is an asynckTask and 2 methods,which are being called by 2 activities.
i Want to keep the AsyncTask class and the methods inside myApplication class
http://developer.android.com/reference/android/app/Application.html
( which was needed anyway,had some states of app to be maintained).
One other way is to have those methods in each activity and the asyncTask as independent class.
what is the best way?
How about having a base activity class for that?
Something like:
public class BaseActivity extends Activity {
protected void myMethod() {
// do what ever
}
}
Then just extend this BaseActivity to have that method in your activities.

RoboGuice #Inject

In my Android app project, I am using RoboGuice .
In my project, I have a singleton Class A:
#ContextSingleton
public class A{
…
public void method1(){…}
}
Then, I have another class B which needs an instance of A, so, in RoboGuice way, I normally declare the instance of A inside class B with injection :
public class B {
#Inject private A a ;
public void action(){
a.method1(); // call method1() of class A's instance
}
}
Sometimes, I got NullPointerException for the instance of A declared in class B. I just want to verify one concept of RoboGuice:
Is it so that in order to inject an instance of a custom class (e.g. class A) in class B, the class B has to be either injected in RoboActivity or be injected into another class (e.g. Class C) which has injected in RoboActivity?
You probably instantiate B somewhere yourself (new B()) and then you need to call the Injector manually.
When RoboGuice creates the instance B it will automatically inject the dependency A, but when you create B yourself, RoboGuice wil not know about it and you have to call the inject code yourself. This can be done by calling:
RoboInjector injector = RoboGuice.getInjector(context);
injector.injectMembersWithoutViews(yourObjectB);

Can I create an object of an Activity in other class?

I have defined a function in MainActivity now I want to access the function from another class in my app. I have created an object of the MainActivity and with that object I have called the function. Although there is no error, it's not executing. Every time I try to execute, the app crashes.
Activity A should have a variable
static ActivityA activityA;
In onCreate state:
activityA = this;
and add this method:
public static ActivityA getInstance(){
return activityA;
}
In activity B, call
ActivityA.getInstance().myFunction(); //call myFunction using activityA
You cannot just create objects of Activities by using:
MyActivity activity = new MyActivity();
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.
Make the variable public and then create object in adapter like this:
public int i; // Variable in Activity class
((ActivityName) context).i // accessing in adapter

Categories

Resources