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

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

Related

What is the use of Context to start an activity in Android?

When starting another activity, the method startActivity(someintent) is used, that intent, at the same time, contains the parameters this for context and a class object.
What is the use of the this parameter, given that the context is already known since the activity itself is a subclass of context? And, given that I create an intent as:
Intent myIntent = new Intent(this,someclass.class);
Am I only able to use the method startActivity when inside this context specified when creating myIntent. In other words, I can't use startActivity with the same intent from another activity.
Several reasons:
You are not required to use this as the context when creating a new Intent in an activity. (You might, for instance, create a ContextThemeWrapper to apply a separate theme.)
The Intent constructor can be called from outside an Activity. In any event, the Intent constructor has no way of knowing what object is calling the constructor (if any—it could even be called from a static context).
There are other uses for an Intent besides calling startActivity().

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 can I get a packagemanager object if i am not extending the activity class in Android/ Java?

i am trying to set up a helper class that does not extend the Activity class. How ever i need to get a packagemanager object with getPackageManager(). How can i do this?
Why I am needing this? Well i am trying to setup this Helper class and I tried doing it with my Helper class that extended Activity. However I always got a nullpointerexceptions since my helper object was null.
Any tips?
Usually the best way to do what you're trying to do it pass the Context to the constructor of your new class. That way you can then use that to call methods which belong to the Activity.
An example is below, it should help you work out your problem:
private Context context;
// ...
public HelperClass(Context c){
context = c;
}
// ...
context.getPackageManager();
You can then call it by just adding your Context when you initialize the new class:
new HelperClass(this);
Although in certain situations, such as from a Fragment, you may have to provide getActivity();.
new HelperClass(getActivity());
Try edit your class as shown above and let us know if you see any improvements.
As mentioned in the comments, unless you're planning on using your context to edits views etc. you'd be better off with context = c.getApplicationContext(); in your constructor.

Problem with intent

I have 2 different class... de first one (MainSoup) is the main class and this class extends activity.
The second class (View2) extends View.
in View2 class is where i make my OnTouchEvent and my Canvas...
I also have a frameLayout with 2 layout... in the first one i put multiple textViews.
On top of this first layout i put the second one wich has nothing and here is where i draw with my Canvas and touch events. At this point everythings works just fine.
The problems begin when i want to make an intent... I put the intent in de Main class (MainSoup):
Intent i = new Intent(this, org.me.androidsoup.MainSoup.class);
startActivity(i);
but i dont know how to trigger it (since the OnTouchEvent is in the View2 class).
And if i try to put it in the View2 class, i have troubles with the startActivity line, It doesnt recognize it and tells me to create a method call startActivity.
startActivity() is a method that requires a context (it's actually a method defined by the Context class).
Views have a method called getContext() that will return the context attached to that view. You could use that for invoking the Intent.
Hope it helps.
Thinks briefly about it gave me 3 options:
You take care of the onTouchEvent on the Activity, (you do findViewById and set the onTouchEvent to it, then you have context)
You add a static Context variable to the Application Class, when each activity starts (onCreate) it sets the Contexts as this like:
Application.context = this;
Use the getContext() method on the View to get the Context, and do new Intent(getContext, ActivityYouWant.class);
I think the first and third are the most valid options. But you choose.

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