Currently I have an AsyncTask declared both in my main activity class and in my widget class. The code is the same except for the result handling in onPostExecute (in activity the result retrieved goes to a textview within activity and in the widget it goes to a textview in my widget).
I'd like to move the AsyncTask to an external java file (to avoid code duplication) and somehow call it from other different classes (like my widget and main activity) with different result handling. I guess I can't just pass a method name as a parameter to call it from onPostExecute, but is there something else I can do?
The short answer to your question is NO, you cannot do anything about this. In general, AsyncTasks are Activity-specific, and are defined as inner classes inside different Activitys so that they have access to methods defined in that Activity.
You can go ahead and define the same AsyncTask inside the Activity and the widget.
Related
Using java with android studio, I'm trying to be able to change the app's orientation from places besides the MainActivity. I can pass the context to a (non-static) field of another class, and call it's methods such as getSystemService(), etc, but it won't let me call setRequestedOrientation() from anywhere but the main activity and I don't understand why. Does anyone know an answer?
For example:
from the mainactivity which extends AppCompatActivity i can call this:
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
And if I pass the context like so from mainactivity:
U.Orientate hey=new U.Orientate(this);
I cannot call context.setRequestedOrientation(int) from Orientate's constructor, the method is not even there.
I understand professionals use the former to tailor the onClick method to their exact specification, but cant I just use the onClick attribute and specify the same desired actions within a method.
i.e. [In xml file:] onClick="doSomething" & then
[in MainActivity.java:]
public void doSomething(View view){
//define and start intent
//Show a toast, etc
}
Why not just use the latter option? Thanks
Why not just use the latter option?
Go right ahead.
On bigger projects, it is unlikely that the activity will be managing the button directly:
You might be using fragments, in which case the fragment is more likely to manage the button than is the activity
You might be using MVP, MVVM, MVI, etc. GUI architectures, in which case some other "presenter" object is more likely to manage the button than is the activity
You might be using the data binding framework, in which case you might still be using android:onClick in a layout resource, but directing the event to something other than the activity (e.g., a fragment, a presenter)
And so on
Yes you can do so,both are doing the same thing but using the onClickListener is a better choice ,you can set the listener when you want that button to be active thus your Button and Activity are in cooordinaton.
If you use onClick in your xml file ,it will always call that method doesn't matter what is happening in your activity.
I was trying to build a typical ListView using the default fragment view in Android - so I need to use setContentView() first to after get the appropriate ListView in that layout. However, I keep getting this error:
Cannot make a static reference to the non-static method
setContentView(int) from the type Activity
I understand this error but do not know how to fix it here (I cannot just go and transform it to static). I am sure my layout name is correct (R.layout.menuList). I am executing this in an AsyncTask under the onPostExecute() section (so it is the same thread as the UI). What am I missing here?
My first thought: Although you've not mentioned but since its complaining about static reference, I assume that you are trying YourActivity.setContentView(R.layout.menuList); ?
Instead try using YourActivity.this.setContentView(R.layout.menuList);. You need correct context.
I read quite some articles about fragments, but I am still confused about how to do what.
I have a MainActivity, which displays two fragments side by side. In one of the fragments I have a button and defined in the fragments layout XML for the button
android:onClick="buttonClicked"
Now I want to implement that method
public void buttonClicked(View view)
I would have assumed that this has to be implemented in FragmentA.java and not in MainActivity.java. But it only works if that method is implemented in MainActivity.java. Why is that? To me that doesn't make sense. Pre Honeycomb a method belonging to one activity stayed in that activity, now on a tablet I am merging many activities to one MainActivity and all the different methods are merged? Whatever do you put for example in FragmentA.java then? What if you have to start you an own activity because this app runs on a handheld, then the onClick method has not to be in the MainActivity but in the Activity which needs to be called then. I am pretty confused at the moment...
I'm not sure what the specific problem is, but maybe this will help.
From the Android documentation on Fragments:
You should design each fragment as a modular and reusable activity component. That is, because each fragment defines its own layout and its own behavior with its own lifecycle callbacks, you can include one fragment in multiple activities, so you should design for reuse and avoid directly manipulating one fragment from another fragment.
That is, you should never manipulate a fragment from another fragment; rather, this should be done through the underlying Activity. Read the "Creating event callbacks to the activity" section in this article for more information (it's important stuff!!).
On the other hand, if you want the button to perform an action within the Fragment itself (i.e. if you wanted a Button click to change the text of a TextView within the Fragment), you should implement this in the Fragment, not the Activity (this is because the resulting behavior is contained within the Fragment and has nothing to do with the parent Activity).
Leave a comment and I can clarify if my post is confusing... I only recently began to understand Fragment's myself :).
Well,
I guess it is related to hierarchy of android context structure.
Activity is host of all child views and hence you can say fragment is actually using its host's context.And that's why when you use onClick with fragment system always searches it in Host activity of fragment.
Check it on.
Android developer onClick attribute description
I haven't checked one thing but you could put a test.
By providing implementation in host activity rather than in fragment,but use onClick on layout file of fragment.It should call parent's method.
I want to update textview in my main activity view.
I have main.xml file that contains the main application ascreen,
in that screen, I have textview that I need to update from time to time from another activity (class).
If I try to update those values from the main activity it work perfect, but when I'm trying to do it from deferent activity the application crashes.
Here is the method of the main activity, I need to know how to call it from the other activity.
Main Activity method:
public void update_counters(){
TextView sms_textview = (TextView) findViewById(R.id.sms_textview);
sms_textview.setText(String.valueOf(sms_missed));
}
Please Help
Indeed it's a bad practice to have any static references to activities (or other contexts). Activity are designed to be rather independed from each other.
You can receive a result from an activity that was started with startActivityForResult() method and than react appropriately.
Like #Roman said, it's a bad practice to touch the UI stuff from other activities, and activities should be independent. What you can do, is a little bit redesign your message passing method. One might be good is to use the broadcast receiver, it also guarantees a better extensibility of your program.
So, whenever your 'other' activity need to call the update_counters, it turns to broadcast such an intent. Your previous MainActivity should register to listen to that broadcast, and update the UI as necessary. What would be the best is you can have several more instance of that activity, or other activity that can register to that broadcast as well.
Is there only one instance of your main activity? If so, store a static reference to it in the main activity class, initialize it in onCreate. Then have a static method that uses that reference to get to the instance:
static MainActivity TheMainActivity;
static public void update_counters()
{
TextView sms_textview = (TextView) TheMainActivity.findViewById(R.id.sms_textview); sms_textview.setText(String.valueOf(sms_missed));
}
The in the other activity:
MainActivity.update_counters();
This is called a singleton. Or, sometimes, a global.