I am not sure if this is the right way to do it. I have a ChatActivity using FrameLayout in frame.xml. This activity needs to be reused across 5 activities. Is there anyway to do a code reuse? This acitivty runs independently of other activities.
For example, in activity A, which uses main.xml, I want ChatActivity and frame.xml to be included. What is the best way to achieve this besides merging the ChatActivity and frame.xml into activity A and main.xml? Merging the activities would mean that I have to copy and paste codes 5 times into different activities. I am not sure if this is the right way...
You cannot "include" an Activity to another Activity. Since your Activity has a basic functionality that all your other Activities use, you could have all your Activities extend this basic Activity.
The best way though for you would be to use Fragments and the compatibility library.
Regarding layouts, you can have reusable ones and import them to your current layout using include.
Hope this helps!
Use Fragments. you can create one Fragment and use the same in all activities. Fragments have their own view. Look at the following link for fragment.
http://developer.android.com/guide/topics/fundamentals/fragments.html
you can use fragments before 3.0 by including compatibility library.
http://android-developers.blogspot.com/2011/03/fragments-for-all.html
I android you can reuse xml files by using the include tag like
<include layout="#layout/okcancelbar_button" android:id="#+id/okcancelbar_ok"/>
To share functionalites of an activity with other activities, create a base activity with common functionalites and make the other activities extend from it.
Related
I've just updated some things on eclipse ADT and I created a new project.
I've seen that in the place where you can set the Main Activity's and main layout's names, now there is another place to set a fragment's name.
When finishing creating the project, in the layout folder now appears the main layout and a framgent layout.
Also, the mainActivity extends ActionBarActivity, makes a fragment transaction in the onCreate() method, and implements the PlaceholderFragment class.
Is there a place where these changes are explained or could someone explain how these modifications affect to coding?
Sorry If this is stupid question, but I'm learning about this and I would like to know how theses changes affect.
Those files are template file for an android application using fragments. Like mentioned before you can delete them or use them as you see fit. Fragments are a little different than regular activities and I would recommend reading the android developer guide on fragments if you want to work with these files:
http://developer.android.com/guide/components/fragments.html
http://developer.android.com/training/basics/fragments/creating.html
These will give you an idea of how fragments differ from regular activities.
Is it possible? supported?
I want to make an app that hosts widgets in fragments, I know how to do it on activity but don't know if it works with fragments.
Im sure you mean AppWidgets, so take a look at this
https://github.com/DagW/FragmentHomescreen
It contains a complete example project for adding widgets to fragments in a viewpager.
It also contains code for adding appwidgets programatically.
Is there way to inherit two different activities in android. I have to display the Map on my activity which inherit from some other activity. i want to display a map on that activity but i can't display the map without inheriting MapActivity. Is there any other way to display the map without using MapActivity.
No, android is java based. And java do not support multiple inheritance.
Java supports multiple interfaces.
Maybe using fragments will help. Fragments could simulate multiple activities. However, all "activities" must be available as fragment. I am not sure whether there exist one for maps
java does not support Multiple Inheritance, although you can come up with a clever design that will let you use functionality of multiple objects in your Activity.
read following article
http://csis.pace.edu/~bergin/patterns/multipleinheritance.html
I modified the pattern listed by Mayank to assume that one base Activity doesn't change. I also made some tweak to show how arguments would work, considering activities will need access to base activity. In the following link, assume map activity would be BaseActivityAlpha. Here is my posting: http://www.anotherandroidblog.com/2013/01/03/extending-from-two-activities
I have created a tab fragments in android 2.2 , with a android compatibility support library , now in my application i have few activities some of them are extends Activity class and some of them extends ListActivity.
so how can i convert the existing Activity or ListActivity into Fragments so that i can take the advantage of Fragment features ?
As to create a fragment , one has to extends Fragment class but if an activity is deriving ListActivity then what to do to convert it in a fragment?
You need to review the Fragment documentation and samples on the Android Developers website. This will explain what a Fragment is able to do, and what you should be doing inside of your fragment.
In essence, its a very simple transition over to using Fragments once you have looked over the examples. You will need an Activity to contain the Fragments still.
To make this a lot simpler, I would advise you look into the ActionBarSherlock library, which will allow you to use the ActionBar and SupportLibrary back to 2.1.
To get you started, you will want to use the Fragment and ListFragment classes, which will be very similar to a standard activity, but the life cycles are a little different with a few naming changes.
You could try deriving it from ListFragment
Ok, I am doing something. I have a navigation bar that contains all the buttons for my activities. I have tried the method of Extending the other activities to the "navbar" class.
Here is what I have done so far:
I have extended all the classes to navbar (Except those that will need multiple inheritance).
used an tag in every XML layout.
What I need:
I need classes to handle multiple inheritance
All my classes even those that do not need multiple inheritance, to extend my navbar class.
If multiple inheritance is not possible, I do not mind hardcoding those classes, but multiple inheritance would be very nice :)
thanks in advance.
Java does not support multiple inheritance, sorry.
Rather than roll your own "navbar", you might consider using the action bar on Honeycomb, perhaps then using ActionBarSherlock to support pre-Honeycomb devices from the same code base.
You could do what I did for my app.
Create a "base" activity that all other activities extend.
Fill your menu bar and set up all of the listeners with Intents in it, then allow each activity to handle everything else.
The other option would be to create a XML element with whatever you want in it (if it needs to be more complex than a menu for some reason). You should still use the "base" activity idea for this.
Hope this helps.