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().
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.
How can I create and set a View for a class that doesnt extend activity? My 'MyServiceIntent' class extends IntentService, rather than Activity.
How can I change the current view in the MyServiceIntent class? Currently it just shows Main's view. But I need a new view shown in my MyServiceIntent class.
I tried:
setContentView(R.layout.my_layout)
but I cant call it as my 'MyServiceIntent' class doesn't extend Activity.
I also tried static 'MyServiceIntent' class, but it wasn't able to access the view data from the main class.
Thanks
You cannot.
A Service is an application component that can perform long-running operations in the background and does not provide a user interface.
https://developer.android.com/guide/components/services.html
However, you can still alter the view of a running activity from the service! For this you should bind the activity and the service. See here for some example code
I'm trying to open a new fragment based on a button push in a previous fragment. What's the best way to implement this?
I'm curious if it's Activity -> .add + .commit original fragment - > from that fragment.java .replace new fragment?
Or do I need to pass an intent back up to the activity and create/replace that fragment from the activity?
So summarize: Activity A - > Fragment 1 - > Fragment 2.
I'm also slightly confused on what things I [need] to #Override. I think only onCreate and onCreateView [within each fragment]?
I'm only looking for high-level here; I want to struggle through the code myself.
Fragments are generally unaware of their host so I would use the standard callback method to call your activity and ask it to switch fragments.
Create an interface
Have your activity implement the interface.
Cast the getActivity() call to your interface.
Call the interface method.
This is much cleaner than casting the host activity and calling methods on it. It also means your fragment can be hosted in different activities with no cast errors.
http://developer.android.com/training/basics/fragments/communicating.html
You need to cast getActivity like:
((MyActivity) getActivity())
And then if you have that, you can call a method in yout activity:
((MyActivity) getActivity()).replaceFragments(Object... params);
And inside the method you should do the replace fragment process.
So simply you have the right idea.
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.
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.