i'm a junior programmer. I have a base class that extends activity, i use this class to set up the menu in my app. All my activity classes extends this base class. Now, i have a new class which extend ExpandableListActivity this class's purpose is to show an expandable list and of course when the user is viewing this activity the menu becomes unavailable. So how can i do to be able to use the menu while viewing the expandable list. I know that it's impossible to extend more than one class in Java, so what's the trick?
Thanks in advance
You can use delegation. Extract all methods related to Menu in separate MenuDelegate class.
Your new class will extend ExpandableListActivity, and delegate all methods related with Menu to MenuDelegate. It's useful solution if your Menu logic is simple.
In this case, your other activities will be free, and will be able to extend ListActivity, TabActivity or something else..
http://en.wikipedia.org/wiki/Delegation_pattern
You don't have to extend ExpandableListActivity to have an ExpandableList in your layout. You can extend your BaseActivity and handle the ExpandableList on your own. The ExpandableListActivity is just a helper class.
Related
The UI elements in the MainActivity class need to be preserved and the other class file adds a new UI element to the main xml layout that's used by both classes to differing degrees.
It's understood that you can create a base abstract class and two concrete inherited classes however in this case there a third supporting class for the second (other class) and it requires a handler to function.
For perspective, a button (in activity_main) is clicked and it should launch an activity while maintaining the UI elements used MainActivity. Furthermore the button has it's own class file methods and isn't in MainActivity.
What happens now? The button is pressed and nothing happens. Manifest confirmed so its not that. Or I allow the main activity or the other activity and it works, both need to work simultaneously.
Basically MainActivity needs to act as the base abstract activity for the separate class file.
You are messing up activities and views. To reuse the same business logic, you can write common logic in base class for all other activities (i.e. class BaseActivity extends Activity). To reuse different UI parts you should either use fragments or you can use <include>/<merge> tags pair to include a certain layout into another layout.
To use fragments in an activity, we need to extend Fragment in that activity. But my activity already extends another class, and java cannot extend 2 classes. So how to use fragments inside an activity that already extends another class?
Multiple inheritance (more than one parent class) was tried in C++ and gave some interesting challenges, so the Java designers decided to try "interfaces" instead.
For your actual problem, have a Fragment object inside your own object and invoke the methods you need.
I am trying to implement an options menu for my app and the same menu is used in different activities. In the Android developers site, it says the following:
Tip: If your application contains multiple activities and some of them
provide the same options menu, consider creating an activity that
implements nothing except the onCreateOptionsMenu() and
onOptionsItemSelected() methods. Then extend this class for each
activity that should share the same options menu. This way, you can
manage one set of code for handling menu actions and each descendant
class inherits the menu behaviors. If you want to add menu items to
one of the descendant activities, override onCreateOptionsMenu() in
that activity. Call super.onCreateOptionsMenu(menu) so the original
menu items are created, then add new menu items with menu.add(). You
can also override the super class's behavior for individual menu
items.
My activities extend from Activity, ListActivity or MapActivity, so what would be the correct way to implement what they are suggesting here? is it possible? Because I cannot extend this new class for all of these, I could only do something like public abstract BaseMenu extends Activity (as explained in this question) but this doesn't work for me. So I am wondering if there is a work around I can implement.
Thanks in advance
For that common Base menu class you need to extend MapActivity class . So you can extend that common base menu class for you all activities.
For that ListActivity you can also implement the list without ListActivity, you can implement it by only Activity or MapActivity.
you have to declare you listview in xml file with the id like below.
<ListView
android:id="#+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Then you have to declare it in your activity class .
ListView listView = (ListView)findViewById(R.id.listView);
listView.setAdapter(your adapter);
Like above you can implement it without extend ListActivity.
you cannot use mapview without extending mapactivity class.... refer this..
MapView without MapActivity ..so you should let your base class extend map activity... and for activity using listview.. put listview in xml and you can use it in your activity..
I would create a static MenuProvider class that implements your onCreateOptionsMenu() and onOptionItemsSelected() methods statically to be called from onCreateOptionsMenu() and onOptionsItemsSelected() of the activities, like so:
public static class MenuProvider {
// You can pass it the activity and other variables used by this method if
// you need to.
// Since the implementation is the same across all activities, they should
// pass the same variables.
public static void onCreateOptionsMenu(MenuItem item, Activity callingActivity, ...)
... // Do stuff on create
}
public static void onOptionItemsSelected(MenuItem item, Activity callingActivity, ...)
... // Do stuff on item select
}
}
And in each of your activities, you would do:
public class MyMapActivity extends MapActivity {
...
public void onCreateOptionsMenu(MenuItem item)
MenuProvider.onCreateOptionsMenu(item, this, ... /*other variables */);
}
public static void onOptionItemsSelected(MenuItem item)
MenuProvider.onOptionItemsSelected(item, this, ... /*other variables */);
}
...
}
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.