How to manage activity inheritance in Android Development - java

This is a relatively general question that I have regarding to Android development.
In the Android application, I am using SlidingMenu library. Imagine the activity I am trying to implement has a Navigation Drawer (from Sliding Menu Library) and action bar tabs with View Pager and contains different fragments.
In order to have the navigation drawer in the activity, I had to inherit the application from SlidingActivity like this:
public class ActivityMain extends SlidingActivity implements TabListener {
However, to make View Pager work in this activity, I will need to use Make a FragmentPagerAdapter instance, and if I want to use it, it requires the activity extend the Fragment Activity.
My Activity already extended SlidingActivity, so there is no way to extend another super class. I am not sure what will be a proper way to solve this conflict. When I was working on the Android app, I have saw some other cases that different components in one activity requires to extend from different super class. What will be the general solution to such problem?
Thank you

If you look at the package of SlidingMenu here, they have an activity called SlidingFragmentActivity. Extend this activity instead of SlidingActivity gives you everything you need.

You can wrap your Activities in a SlidingMenu by constructing it programmatically new SlidingMenu(Context context) and then calling SlidingMenu.attachToActivity(Activity activity, SlidingMenu.SLIDING_WINDOW | SlidingMenu.SLIDING_CONTENT). SLIDING_WINDOW will include the Title/ActionBar in the content section of the SlidingMenu, while SLIDING_CONTENT does not. You can check it out in the example app AttachExample Activity.
from https://github.com/jfeinstein10/SlidingMenu

Related

what exactly is AppCompatPreferenceActivity?

I have no idea of programming. I would like a very informative answer so i can fully understand what this class is about. I found this"implements and proxies the necessary calls
* to be used with AppCompat."
But i don't no either what is AppCompat.
PreferenceActivity :
It is the base class for an activity to show a hieracrchy of preferences to the user.
Example : To enable / disable notification , data sync frequency, etc.,
AppCompatActivity :
It is the base class for activities that use the support library for action bar features.
You can add an ActionBar to your Activity when running on API level 7 or higher by extending this class for your activity and setting the activity theme to Theme.AppCompact or a similar theme.
Example: Provides Material color themes, widget tinting, appbar support, etc.,
AppCompatPreferenceActivity :
It is an abstract class, which extends PreferenceActivity.
When you create a new preference activity from the menu, android studio itself add this class.
Example : Create SettingActivity from menu, it will extend from AppCompatPreferenceActivity.
This class has more useful methods (inflate, set / get support actionbar etc).
Example :To add the toolbar, inflate the toolbar and set it. Only minimal lines of code .
Hope this helps you !!!

Batch edit activities in Android Studio

I have recently decided to add a Navigation Drawer to every screen in my app. I already have the working code. Is there a faster way to add the same layout to every screen or must I do this manually?
One way you could do it is to create an Activity or Fragment class say BaseActivity that all your Activity or Fragment will inherit, and put your Navigation Drawer logic there.

Bind listeners on customized menu across activities in Android

I am new to Android development. I want every activity has a same customized popup menu on the top right. I use a LinearLayout in xxmenu.xml and include it in each activity's layout xml files. The display is OK. As the OnClickListener should be same, I don't want to implement the OnClick functions in every activity's Java file. How can I do this?
I know Android has inherent menu solution via OptionsMenu, but I don't know if it can be customized or how (e.g., I want it on topright corner, with different backgrounds, each item has an image icon followed by texts, one-side shadow, etc.)
Make an abstract BaseActivity class that implements onCreateOptionsMenu and onOptionsItemSelected for your common menu and menu items. Then make all your other activities extend from that one.
First, the solution is to define a base class (e.g., BaseActivity) to extend Activity, and the actual activities using this menu to extend BaseActivity (e.g., MainActivity extends BaseActivity).
Second, use a separate xml to define the menu layout and include it in other layouts. A simple way to bind the listeners is to use android:onClick="toggleMenu".
Third, implement public void toggleMenu(View v) in BaseActivity.
We can also call the views and do the bindings in Java code. But I met a code sequence problem that if I get the clickable view via findViewById in BaseActivity's onCreate(), it returns null, and the binding fails. So the finding of the views and following bindings should be done in the child classes (e.g., MainActivity), after calling super.onCreate().

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

Action Bar as Fragment

I have just started developing an Android app and have no experience at all. I have read a lot about Activities / Fragments / Widgets, but don't seem to find a clear answer to my question which is:
Can I create the Action Bar for the app as a fragment so whenever I change an activity I will simply call the one action bar (i.e. the one fragment)? I intend to develop a dynamic UI to create fragments for individual option and thought that it would be easy to have a general Action Bar appearing on all pages.
When you want to customize the ActionBar in all your Activities, the first step is to create a custom Theme in XML.
In this theme, you can customize nearly everything
Please refer to this excellent blog post: http://android-developers.blogspot.be/2011/04/customizing-action-bar.html
Using a Fragment for the ActionBar would be crazy!
If you want to add some code programatically in all your Activities, simply extends a custom Activity, for instance MyCustomActivity, that extends Activity.
public class MyCustomActivityextends Activity{
In this class, you can use getActionBar() and tweak it according to your needs
If you wanna have one ActionBar for all your Activities use inheritance. Create an Activity which simply handles the ActionBar like you wanna have and make it the Superclass like this.
public class ActionBarActivity extends Activity{
public void onCreate(... ) {
ActionBar actionBar = getActionBar();
// + some other method calls of your choice
}
public onCreateOptionsMenu(Menu menu){
// create your actionbaritems here
}
public boolean onOptionsItemSelected(MenuItem item) {
// handle your click events for the items here
}
}
Now you can use this Activity for all your Activities with inheritance:
public class MyActivity extends ActionBarActivity{
...
}
With this setup you are free to use Fragments as you like.
Keep in mind that every time you call a new Activity the callbacks of the super class will be invoked.
I think what you're thinking of is this:
You want your action bar to be the same on every screen, and only need to program it once.
The approach I use, is that the actionbar live in a root activity, containing a single viewpager. And all of the screens that the user interacts with are Fragments in that view pager.
If you create a blank android project in eclipse, and select actiobar with tabs, the project will set this up for you and you can see how that works.
In most cases this would be an unnecessary complication. Once you get started, you will probably find that just a few simple lines of code will create an "identical" Action Bar in each activity .Themes etc can be added later. Better questions at this stage might be should I use Action Bar Sherlock to enable better support of old devices ? and you should think about the overall structure of your app e.g activities / fragment activities / fragments / tabs so that you are able to get something working quickly that can be readilly extended as you develop you full solution.

Categories

Resources