0

I want to know if Android support the possibility to make a container with some static components and just include my activities in it.
I have a sliding menu with some onClickListener events and I don't want to set these events for each activity.

Adinia
  • 3,722
  • 5
  • 40
  • 58
OWZY
  • 531
  • 7
  • 15

4 Answers4

3

If I understood you correctly, you have some functionality that is common to several Activities, and you don't want to repeat the same code in all of them. Instead, you want to do that in one place.

One way to achieve this is to create a superclass activity, place your common code in that activity, and then extend it with your other activities. For example,

public class BaseActivity extends Activity implements OnClickListener {
private Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  button = (Button) findViewById(R.id.my_button);
  button.setOnClickListener(this);
}

  ...

@Override
public void onClick(View view) {
   int id = view.getId();
   switch(id) {
     case R.id.my_button:
     // perform action
     break;
   }
}

}

Then you extend it as

public class Activity1 extends BaseActivity {
  /...
}

public class Activity2 extends BaseActivity {
  /...
}

public class Activity3 extends BaseActivity {
  /...
}
Nazar Merza
  • 3,365
  • 1
  • 19
  • 19
  • it gives no effect. I instantiated my slinding menu in BaseActivity but it doesn't toggle in HomeActivity (extends BaseActivity). – OWZY Feb 19 '13 at 21:28
  • You need to post your code. Otherwise, it is hard to see what is happening. – Nazar Merza Feb 19 '13 at 21:39
  • it's okey now, I had to attach the menu to the activity. thanks for your help – OWZY Feb 19 '13 at 21:40
1

I am not exactly sure I understand your question, can you perhaps elaborate some more? maybe even post some sample code that you are using currently.

From what I can tell you should be able to achieve what you want by making your own CustomActivity

public class CustomActivity extends Activity {
  //put your slidingmenu stuff here
}

Then inside all of the other Activities where you want to use that shared piece do it like this:

public class AnotherActivity extends CustomActivity {
  //...
}

with extends CustomActivity instead of the usual extends Activity

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
  • I already tried it, but it gives no effect. My Sliding menu is a library that I instantiate in my main activity. – OWZY Feb 19 '13 at 21:13
  • You're going to have to post your code. Im having trouble figuring out what you are trying to accomplish. – FoamyGuy Feb 19 '13 at 21:15
  • I found a method that allows to attach menu instantiated in base activity in the current used activity (attachToActivity). thanks for your help – OWZY Feb 19 '13 at 21:44
0

This is how i solved the problem:

First thing i did is creating my main class wich will host common code. for example :

public abstract class main extends activity(){
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
        // Your common code here
    }
    protected abstract int getLayoutResourceId();
}

Then all what you need is to extend this class in your activity:

public class HelloActivity extends main{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_hello);
        super.onCreate(savedInstanceState);
        //make sure to put setcontentview before super.oncreate
    }
    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_hello;
    }
}
IAbstract
  • 19,551
  • 15
  • 98
  • 146
OWZY
  • 531
  • 7
  • 15
-1

All the activities needs to be registered in the Manifest. For the common things, e.g. Slide menu, you can use Fragment for the slide menu.

Gaurav Arora
  • 1,805
  • 13
  • 13