Unknown method getIntent - android

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.

Related

Get activity instance from called activity

I have an activity containing some fragments. One of those fragments calls another activity. In this new activity I need to have an instance of the first activity. GetParent() returns null so I don't know how I can acomplish this...
MainActivity --contains--> Fragment1 --startActivity()--> SecondaryActivity
Is there some way to get the calling activity on the SecondaryActivity?
I don't think there is a good way of doing so.
It's really a bad practise to handle activity references like that, since android wouldn't be able to gc them when needed (orientation changes, lack of memory).
The best way is to pass all the data you need with Intent extras (intent that you use to start activity), and, if you need SecondActivity to return something, use Fragment1.startActivityForResult() for starting activity, and when done, use SecondActivity.setResult() to return desired result, you will need to override onActivityResult() to get the result (there are plenty tutorials about this).
If you absolutely need to hold references to something, you can use your own instance of Application class (don't forget to declare it in the manifest) to hold data for you while application is running.

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.

Difference between myactivity.this , myactivity.class, this

I have been trying to use Intent method in my program , the code shows no error when I use myactivity.this ... when I use the other two (myactivity.class or this),eclipse shows an error.
Intent i = new Intent(myActivity.this,myActivity2.class);
startActivity(i);
When I use myactivity.class or this in the first param,
Eclipse shows an error of Constructor Intent not defined. Why is that, can anyone explain?
myActivity.this == Refrence to context
myActivity2.class == Reference to class, this is its class name
this == It is Current Type, say if you are in Thread then it is Thread Type; if you are in Activity then it is Activity Type; if you are in your custom class say CAR then it is CAR type
When you do the this then you get an error because you must not be in main thread in this you can use getApplicationContext()
When you use myActivity.this It Knows that it will be started from this activitie's context.
Let me give you the answer of:
When i use myactivity.class or this in the first param ,Eclipse shows
an error of Constructor Intent not defined.
Reason you got error is that you are supposed to pass valid parameters to the Intent Constructor that you are trying to invoke.
See this: LINK
Which are
A Context of the application package implementing this class.
The component class that is to be used for the intent.
And as you mentioned, you tried myactivity.class , refering to KITKAT'S answer this parameter is not valid enough to get passed to the Intent constructor.
As for this is concerned, you shouldn't get any compile error, if you are within valid activity context.
The first param is for the current activities context therefore this or Activity.this or getApplicationContext will do. and the second param refers to the class name where you want to move to. That is why
.this in the first param and .class in the second. Hope you got it now.
Maybe you are writing this code in another object ,like in OnClickListener ,thus this is representing the current object of OnClickListener not the MainActivity class.
That's why you should use MainActivity.class to reference to main Activity.
this in this context is representing the object of OnClickListener.

how to use Activity in sub class

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?

Launching an Intent outside an activity

I have an asynch task with my app which goes to a website, grabs the results from the API and appends a number of clickable textviews to an existing LinearLayout.
However I want to be able to launch a new activity when the textview is clicked. This isn't possible with the asynch class defined in a seperate file, would it be easier to define it as an inline class within the activity?
You can always pass Context to your async class.
A better approach would be to have callbacks (listeners) in the calling class for the async to call back to.
One approach is to inflate your TextViews from an XML file that declares an onClick attribute, naming a method defined in your Activity.
Do not use a context as an Activity! You will probably receive a cast error anyway. Instead, you can pass the activity as a function parameter, like this:
public void function(Activity act)
{
Intent intent = new Intent(act, newActivity.class);
act.startActivity(intent);
}
Or overload the constructor to accept the activity as a parameter. But I strongly suggest you to check you code. If you are calling an activity, you, probably, should be within another one, don't you agree? But, I Know that sometimes we have to make a few concessions, in order to make things work properly. So, use it wisely.

Categories

Resources