using getBaseContext method - android

I have a class which already inherits another class. Now from within this class, I want to start an activity, which needs activity class to be inherited. Which is not possible since my class has already inherited one.
In this situation, whats the best way to start an activity?
Some suggested me to use getBaseContext() method, but its saying "getBaseContext can not be resolved". Could anyone suggest a way...?

create a constructor for this class which takes Activity Instance as arguement.. and if u r creating object of this class in an activity.. pass its instance and then use that to get Context... or u can just say
if its a reciever then its very easy.. reciever gets a context object in its onRecieve method.. which u can use like this
Intent i = new Intent(context, some.class) //activity is an instance of Activity()
i.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(i);

Related

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.

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.

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.

Android, trying to call a new intent from a touch event

I'm trying to make a app that when the screen is touch it will call up a new intent.
I have code that catches the touch event in the view class. When I try to create a new intent, Intent(this, cYesNoDisplay.class);, i get a error saying the constuctor is undefined, I'm assuming the constructor is not defined in the view base class, but the Activity class?
I'm confused about how to do this, is there a way for my View class that is a member of the intent class, to call it some how???? I figure there must be a wy to do this, still learning Java.
Ted
Your assesment about the View class that you are inside of being the problem is correct. In order to get it working do this:
Intent i = new Intent(NameOfYourActivity.this, cYesNoDisplay.class);
replace [NameOfYourActivity] with the name of the activity that you are inside of.
EDIT: I might have misunderstood what you were doing. If you have actually built your own View class and are overriding onTouch() you actually need to do it a little bit differently.
If you don't already have it add:
Context ctx;
to your classes declarations.
in your constructor alter it to store the context that gets passed in as a parameter in the ctx reference that you declared.
public [ClassName] (Context c){
this.ctx = c;
}
Then inside the onTouch() do it like this:
Intent i = new Intent(ctx, cYesNoDisplay.class);
ctx.startActivity();
EDIT again: The reason you have to use ctx.startActivity(i); is that startActivity() is a method of Context. Since Activity is a Context you don't have to put anything in front of it when you call it from inside an activity. But when you are "inside" of a different class you have to use a reference to a Context to call the method.
Use this . this work fine in my project.
Intent i = new Intent(NameOfYourActivity.this, cYesNoDisplay.class);
startActivity(i);

calling oncreate within constructor

I am calling a class's member function from another class. So,I am creating an object of the class for which i have to call constructor. Within that constructor, I need to call onc reate method of my activity class.how can I do it?
Do not call onCreate from your constructor. The system itself will call onCreate for you.
Take a look at the Activity Lifecycle for more information of how onCreate is called.
Well,i presume u r trying to call a constructor in an activity.If that is the case then some thing is wrong in the way u have designed ur project.For more details check out these links
Creating an object of Android Activity class and Android - Activity Constructor vs onCreate
All method's of Activity class should be called within activity only . write your own methods for whatever operation you need to perform and pass data from other Activity/class by various data transfer algorithms .

Categories

Resources