Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:50:35+00:00 2026-05-26T14:50:35+00:00

I’ve been learning android development (in Eclipse) the past week. I’m following online courses

  • 0

I’ve been learning android development (in Eclipse) the past week. I’m following online courses and reading books as much as possible, whilst also digging in and trying to write an application. I’m playing with GreenDroid in an application and I have a real life example to run by you.

I have my main application class as follows:

public class InfoActivity extends GDListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.info_activity_title);

    ItemAdapter adapter = new ItemAdapter(this);
    adapter.add(createTextItem(R.string.info_about, AboutActivity.class));
    adapter.add(createTextItem(R.string.info_terms, TermsActivity.class));
    adapter.add(createTextItem(R.string.info_privacy, PrivacyActivity.class));

    setListAdapter(adapter);
}

private TextItem createTextItem(int stringId, Class<?> klass) {
    final TextItem textItem = new TextItem(getString(stringId));
    textItem.setTag(klass);
    return textItem;
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    final TextItem textItem = (TextItem) l.getAdapter().getItem(position);
    Intent intent = new Intent(InfoActivity.this, (Class<?>) textItem.getTag());
    startActivity(intent);
}
}

Everything works well. Here’s my question – how can I re-use the code from one class in others but take a variable and change it based on that? – the line

adapter.add(createTextItem(R.string.info_about, AboutActivity.class));

requires another activity class to be created. The code for that is as follows:

public class AboutActivity extends GDActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(R.string.info_about);
    setActionBarContentView(R.layout.info_template);

    WebView engine = (WebView) findViewById(R.id.web_view);  
    engine.setBackgroundColor(0);
    engine.setBackgroundResource(R.drawable.bg);
    engine.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    engine.getSettings().setBuiltInZoomControls(true);
    engine.loadUrl("http://m.example.com/about.html");
    engine.setWebViewClient(new WebViewWithin());
}
}

The other two classes TermsActivity and PrivacyActivity are identical except for two parts:

setTitle(R.string.info_about);

and

engine.loadUrl("http://m.example.com/about.html");

Which in the latter activity example would be:

setTitle(R.string.info_privacy);

and

engine.loadUrl("http://m.example.com/privacy.html");

As this is my first time learning Java, I’m still getting my head around lots of the concepts. I’m well versed in PHP and would write a function that could take a string variable and place it into the function (effectively re-using the same code), such as:

This is PHP and Java pseudo-code to get my point across.

<?php 
function aboutSecondaryPage( $page ) {
  setTitle( this.getString( R.string.info_ . $page ) );
  engine.loadUrl("http://m.example.com/" . $page . ".html" );
}
 ?>

So given these examples, is there a better way for each of these three classes to re-use the same code and call it from another class. Any guidance would be useful. As is, everything works, I’m just seeking to learn from those with much greater experience.

Thank you!

CURRENT SOLUTION (AFTER KURTIS EXCELLENT ASSISTANCE)

My parent activity method which the other three activities inherit from:

public class InfoSecActivity extends GDListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTitle(getTitleResourceId());
    setActionBarContentView(R.layout.info_template);

    WebView engine = (WebView) findViewById(R.id.web_view);  
    engine.setBackgroundColor(0);
    engine.setBackgroundResource(R.drawable.bg);
    engine.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
    engine.getSettings().setBuiltInZoomControls(true);
    engine.loadUrl(getUrlToLoad());
    engine.setWebViewClient(new WebViewWithin());
}

public int getTitleResourceId() {
    return 0;
}

public String getUrlToLoad() {
    return null;
}

}

An example of sub-class which is called from InfoActivity class.

public class AboutActivity extends InfoSecActivity {

public int getTitleResourceId() {
    return R.string.info_about;
}

public String getUrlToLoad() {
    return "http://m.example.com/about.html";
}

}

It works beautifully, but in the interest of learning best practices – have I done this the best way and anything to be aware of (potential problems with this methodology)? This is mainly for Kurtis – hope you read this again 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:50:35+00:00Added an answer on May 26, 2026 at 2:50 pm

    You could always take the code that’s getting reused and extract it up into an abstract parent class. So in this parent class you’d put all the code that is shared by all of your activities. But where the code differs, you call abstract methods that you implement concretely in each of the subclasses. This is called the Template Design Patter. So for instance, in your onCreate method of your abstract parent class you’d do this:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(getTitleResourceId());
        setActionBarContentView(R.layout.info_template);
    
        WebView engine = (WebView) findViewById(R.id.web_view);  
        engine.setBackgroundColor(0);
        engine.setBackgroundResource(R.drawable.bg);
        engine.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        engine.getSettings().setBuiltInZoomControls(true);
        engine.loadUrl(getUrlToLoad());
        engine.setWebViewClient(new WebViewWithin());
    }
    

    In this case, you would then declare getTitleResourceId() and getUrlToLoad() as abstract in the parent class. Then in each subclass, the only piece of code you have to write is the definition for the getTitleResourceId() and getUrlToLoad() methods. For example, in your Privacy activity you’d have these methods return R.string.info_privacy and "http://m.example.com/privacy.html" respectively. But in your other subclasses, you’d have them return different values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am reading a book about Javascript and jQuery and using one of the
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.