Question

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...

Was it helpful?

Solution

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!

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top