Why can't I call context.setRequestedOrientation(int)? - java

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.

Related

Same asynctask but different result handling in onPostExecute()

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.

Set BaseActivity with custom constructor to supply data from child class

I have a BaseActivity I use in my app to help with make Google Analytics a bit easier.
In my base activity i override onStart() and onStop() to send some stuff to EasyTracker. I would like to set up my code so that it requires me to make a call through to the superclass constructor with the name of the activity so that the BaseActivity can send the right data along.
I tried setting up a constructor in the BaseActivity, and it seemed to work, but i got the error that my main activity (which extends BaseActivity) hierarchy is inconsistent.
What would I be able to do to achieve this?
You can't do this with constructors. The Android framework instantiates Android components (Service, Activity, BroadcastReceiver, ContentProvider) itself and you can't muck around with that.
My suggestion is that you override onCreate() in your base activity and have your subclasses call super.onCreate() from their overridden onCreate() method. In your base activity you can get the name of the subclass by calling getClass().getName().

Disable landscape mode for a whole application

Is the there a way to set a landscape mode to the whole application, not by adding android:screenOrientation="portrait" to every activity in AndroidManifest?
Here's the only thing I can think of. Write a class that extends Activity and put the following in that class:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Then, instead of extending Activity in your other classes, extend the new class.
One programmatic way of doing this, that I can think of, is to create a super class that extends activity and extend all your classes from there.
Have the below setting in the super class in a protected method and call super.xxx() to initiate this:
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
In case you what a specific activity in a different way you can simply override this.
[I have not tried this yet, but by the knowledge of OOP this works]
You could try putting that attribute in the node of your manifest. I don't know if that is supported though. And if not Im afraid putting it in each of your is going to be the next easiest way.
You might be able to achieve it by making yourself a CustomActivity that extends activity and sets the window flags to be Portrait in the onCreate. Then with all of your other activities you could extend your CustomActivity instead of plain Activity.

android: own class extending activity and/or listactivity

I have a couple of things which are the same in all my Activities throughout my application, e.g. an optionsmenu and some code which needs to run onresume, onrestart and onpause. I figured it would be a smart approach to put them in my a class MyListActivity extends ListActivity and then have all my activities extending MyListActivity.
This worked out just fine until I created an activity which didn't have a ListView. the App crashes because ListActivity expects a ListView. However, this new activity does not need a ListView, but would still need all my functions / Overrides in MyListActivity .
Right now I can think of two solutions. One: add a dummy listview to the layout with visibility = false, height & width = 0 (haven't tried this, but i guess it should work). And Two: copy/paste the contents of the MyListActivity class into a MyActivity extends Activity class. I feel very silly doing this, but I don't have any other ideas on how to solve this issue.
Any ideas on how to handle this nicer?
Thanks
I think you can implement all the features in an Activity subclass (e.g., MyActivity) and make the MyListActivity class a subclass of the MyActivity class.
Other approach is to make a helper class which contains all the features in static methods with an Activity object as the first argument. In this case you don't need to create MyActivity or MyListActivity classes, but you need to call methods of the helper class in every Activity subclass you want to inherit these features.

Update main activity values from a different activity

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.

Categories