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.
Related
I want to fill ("calculate") a ListView in a Loading Activity and when it's ready, start the new Activity with the ListView filled. So I don't have to wait to fill it in the second activity. I already have the code to fill it, but it's in the second activity.
Thanks
First, this is not really possible. You cannot readily transfer widgets or adapters to another activity.
Second, this is not really necessary. Creating a ListView is fairly cheap. If you are experiencing performance anxiety, use Traceview to determine where the problem is, then scope your plan to deal with the problem. For example, perhaps it is your model data, not the ListView itself, that you need to load in advance. Or, perhaps you just need to move some work to background threads, such as loading images to go into the ListView rows.
Third, if these two bits of UI are this closely coupled, they should not be separate activities, but rather one activity. Whether you use fragments, or hide/show widgets, or whatever, is up to you.
Your code for Activities, User Interface that may contain creating custom views , AND code for adapters must be independently defined or in separate packages if u prefer to go for clean coding having done that u will not face above problem and also u can REUSE your code .
What you really want is to transfer the data from one activity to another activity. Instead of passing the "ListView", you should be passing the data that you need to populate in the other activity
Intent intent = new Intent(CurrentActivity.this, OtherActivity.class);
intent.putExtra("SOME_KEY", mySerializableData);
startActivity(intent)
You can pass any type of serializable data using intents.
So you should ideally:
Create your adapter somewhere outside of your CurrentActivity and
OtherActivity
Pass the mySerializableData from CurrentActivity to OtherActivity
Use the same adapter to populate the ListView using the data passed in the intent using getIntent().getSerializableExtra()
Use instanceof to ensure that what getExtras() returned is of the type MySerializableData
So for example if we want to pass a ArrayList<MySerializableData>
Object obj = getIntent().getSerializableExtra("SOME_KEY");
if (obj instanceof List) {
//Do stuff
}
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.
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.
I want to call onCreate(Bundle cicici); from other class then i am getting "NullPointerException", so please guide me how can i call the onCreate() from another class.
There is only one way in which onCreate can be called, by starting an Activity, since onCreate is as part of Activity life cycle.
startActivity(new Intent(presentActivity.this, NextActivity.class));
if you want to call onCreate in order to actually present a new screen, then you need to create a the new Activity using the android framework style.
Ingredients:
1- An event to call your new activity( ie. onClickListener of a Button or list triggered)
2- On the event you need to create an Intent with the reference of the current activity and a class reference of your new Activity, example:
Intent intent =new Intent(CurrenActivity.this, MyNewActivity.class);
3- You need to call this activity depending on what you'll need you use startActivity or startActivityForResult, the last is use when you expect a response from your activity.
You can also refer to Android documentation Common Task, let us know if its helpful
It depends what you want to do in the second activity. If you want to create a simple task you can always use dialogs and you can show them inside your activity.
Or, on a second thought, you can hide some of your views and enable others but I guess that's not an orthodox solution :)
I have an activity which has a static method for updating a textfield. This way I can update this view from another activity.
But now I'm trying to get a Context variable in this static method which is not possible. I've tried declaring a Context variable and initialising it in onCreate ( context = getApplicationContext();)
But still I can't access context in this static method. How is this normally done?
edit: a little bit more information about my situation. I'm starting a countdowntimer in an activity(a) which updates another activity's(b) ``textfield every second. And it does this by accessing b's setTextField in a static way..
How is this normally done?
Accessing a TextView via a static method is not the best way to update the field from another activity. If you want to pass a value to the activity when it starts, you can send data via the intent (i.e. intent.getExtras). If you want to pass data back from a sub-activity, you can use startActivityForResult.
The way you are going is very strange. Why are you trying to change one activity content from another? May be you need to use startActivityForResult to strat a new activity and then return result from it and change views depending on it?
You might want to check some documentation on OO and using static functions. It is not considered a very good approach.
But as we are not talking about a better complete sollution: you can add a parameter with a context to the function, and just give it when you call the function :)
I would suggest the LocalBinder pattern to update the other Activity:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/LocalService.html
Can you do something like this?
something like this <viewobj>.getContext()
Ref: How can I start an Activity from a non-Activity class?
Whenever you're busy with Activity A, there's no point in updating something on Activity B as it is simply not shown to the user at that point in time.
Seems to me you need to have some kind of global variable here that can be picked up in the onResume of Activity B.
Checkout this question : How to declare global variables in Android?
It shows you how to use the Application class to maintain global application state, accessable from all activities when needed.