how to use Activity in sub class - android

Conceptual problem here!
I want to break down a program into simpler more focused classes. The first one I am doing requires a startActivityForResult() which in turn depends on an Activity.
I can pass the Activity to the class. But there is a problem in the caller when I use getActivity() this seems to be undefined for the class.
Why isn't getActivity documented by the way?

Related

How can we access an android activity from within context?

Trying to call a function from an activity, but I need the specific instance of that activity to do so.
Is there a way to call that specific activity from the application?
If not, is there a way to start an activity from the application so that I always have access to the instance I start running? I tried this, and edited the manifest, but the app never started...
As concerned with the limited details in this question, I think your requirement is to call a function in an activity that needs that activity itself as the parameter. I think you can do it like this.
Activity actiity=this;
yourMethode(activity)
{
//body of your methode
}
Whenever you use the variable "activity", you can get an instance to the current activity.
I think you are talking about casting the context to get the type of activity it represents. You can do it like this. But be careful if the context is not of that type you will most likely cause a crash.
((MainActivity) mContext).myMethod();
This is not really recommended as it will cause some tight coupling between the class and the activity.

Unknown method getIntent

I am making an application on AIDE for android, and I'm using an intent to send data from an activity to a normal class.I'm using:
int level= (currentLevel*100)/scale;
Intent i = new Intent(context, caller.class);
i.putExtra("level",level);
context.startActivity(i);
in the class that sends the data ("percentage.class").
int p = getIntent().getIntExtra("level");
in the class that receives the data ("caller.class")
which gives me an error: "Unknown method getIntent()".
What can I do to fix this?
Thanks in advance!
It's easy to say in your question what is the problem. The class in which you are calling getIntent does not inherit the class Activity.
Unlike what other people are saying inheriting Activity is unlikely to give you what you're looking for. What I'm suspecting is that you're calling getIntent in a button or something like this. Since it might be wrapped inside a method that isn't directly pointing to your activity. You should "keep" a pointer to the activity.
Usually, what you are looking for should be in the context. Calling context.getIntent might work if your context is the thing I "believe" it should be. Show more to give us a better idea of what is going on. Because since getIntent is calling from the activity. getIntent is the same as writing this.getIntent but Java implicitely calls function on this and then on the global scope (the thing you import).
If you want to avoid this problem, alway call it from this and when you're calling from within a Handler, you can keep references in your class to the current activity. I'm not so sure but on some object, you should have a function getActivity that will return the activity in which they are located.
you could have something like this. obj.getActivity().getIntent()...
Check this out: What does getActivity() mean?
The getIntent() method belongs to the Activity class. You will get this error if your class does not extend Activity. Try extending Activity and see if it works.

Who will be create on object for Activity?

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:

Android - Parent activity should determine what child activity had called it

I have 2 activities in my project, lets say Activity A, and Activity B. Both A and B extend the same superclass: BaseActivity.
We know that in this case, lets say if the Activity A is opened, then the superclass method onCreate() is called, and then the Activity A's onCreate() follows.
Being in the onCreate() method of the parent BaseActivity class, how can I determine which child activity has been started?
I would suggest a different approach. Basically don't do inheritance. Use composition instead. Remember most operations that you think you need to extend for really just need a reference to the context. Activities extend from context. So really you can provide most base functionality in any class that has reference to an active context. No need to do inheritance at all.
So if you want to share some functionality between ActivityA and ActivityB just put it in HelperC
HelperC.someOperation(Context c, otherParams)
HelperC can do anything that some base activity could do. Ultimately the base activity will never exist any way. It will always be an instantiated version A or B
I don't think you should do it this way.
As far as I remember the idea of extending, superclass method should contain only universal code. Puttin the differenting code in child classes would be much easier to do and as I believe more proper.
You can initilize some values in parent onCreate(), and then re-set it in childs'.

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.

Categories

Resources