0

I have multiple activities in which I want to use same code in those activity. I finished it with having same bunch of code in each activity. How can minimize this redundant code. For doing this I dont want to create object and use it's methods for removing redundancy...

Plz help... Thank U....

9
  • why don't you want to use another object to reduce redundancy? Commented Apr 8, 2012 at 5:56
  • @JacobPhillips : coz I have read, creating objects may cost in terms of resources, performance etc. Its not recommended in mobile application... Hey let me know if I m wrong... I m new to android... Commented Apr 8, 2012 at 6:04
  • I think you should have common ("redundant") methods and include them in a new class that your other activities inherit from. You do want to be conscious of memory usage, but creating a fairly simple object is perfectly acceptable in this (and most) situations Commented Apr 8, 2012 at 6:07
  • You can can create a "BaseActivity" and include a lot code in that single class and then simply create classes and extend that "BaseActivity". Commented Apr 8, 2012 at 6:11
  • @MasterJB : I will use ur option... thankx.. Commented Apr 8, 2012 at 6:20

2 Answers 2

5

the solution is simple: extends the class Activity, and add him all the methods you want. (I usually call this class BaseActivity for instance)
then, when you are developing new activity - instead of extend the class Activity - extends the class BaseActivity you've made, which contain the methods you added..

public class BaseActivity extends Activity
{
protected int mSomeValue;

protected void someMethod1()
{

}

protected void someMethod2()
{

}

protected void someMethod3()
{

}

}

class SomeActivity extends BaseActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    someMethod1();
}
}

class SomeActivity2 extends BaseActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    someMethod1();
}
}
Sign up to request clarification or add additional context in comments.

Comments

5

You can can create a "BaseActivity" and include a lot code in that single class and then simply create classes and extend that "BaseActivity".

Here is what I think you are looking for:

public class BaseActivity extends Activity {

public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}   

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){

    case R.id.donate:  
        //something
        break;

    case R.id.about_menuitem:  
        //something
        break;

    case R.id.exit:  
        finish();
        break;         

    default:
        return true;
    }
    return true;
}   
}

I created a class called "BaseActivity", in this class I have my Android options menu and I also extend "Activity". Since I extended "Activity" and have my options menu in this class, I can now use this same menu code for all my other classes.

I just simply create my new classes and extend them with "BaseActivity":

public class SomeOtherActivity extends BaseActivity {
 //new code here
}

Now the class called "SomeOtherActivity", will inherit my menu code and also "Activity".

Please try this out and let me know is this helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.