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 7702603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:15:51+00:00 2026-05-31T23:15:51+00:00

I have two ListActivity classes that I want to pass into into a third

  • 0

I have two ListActivity classes that I want to pass into into a third class as below. The reason being that I’m using the deprecated android.text.ClipboardManager in one activity for old Android versions, and the newer android.content.ClipboardManager for the other one. These are used in different ways and need to be separated.

Activity1

import android.content.ClipboardManager;

public class Activity1 extends ListActivity {
    ThirdClass tc = new ThirdClass(this);
}

Activity2

import android.text.ClipboardManager; <--- different

public class Activity2 extends ListActivity {
    ThirdClass tc = new ThirdClass(this);
}

ThirdClass

public class ThirdClass {

    public ThirdClass (ListActivity la){
        //do stuff
    }

    //other methods
}

In this third class I want there to be shared code that are used for both ListActivity classes. However, the third class needs to be able to tell whether it’s working with Activity1 or Activity2 so that it can use the specific variables and methods that are defined within these classes. This can be done by casting the class name like this:

((Activity1) la).whatever

My question is, what’s the easiest way to retrieve the class name Activity1 or Activity2 with code from the ListActivity variable “la” that has been passed down to ThirdClass?

The only way I’ve been able to figure out myself is by using getClass() and getName() and then using if statements as below.

String activity = la.getClass().getName();

if (activity == com.stuff.Activity1) {
    //do stuff
}

But there has to be a better way so that I can directly cast the class name to anything using the “la” variable?

UPDATE

Using instanceof would result in a lot of duplicate code since I would have to write it up once for each instance as below. I’m hoping to avoid this.

if (la instanceof Activity1) {
    ((Activity1) la).whatever
}

if (la instanceof Activity2) {
    ((Activity2) la).whatever
}

Using an interface that contains the needed methods like Stefan suggested may be a good idea? I actually don’t know much about interfaces so haven’t considered this. I’ll look into it.

  • 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-31T23:15:53+00:00Added an answer on May 31, 2026 at 11:15 pm

    Do it with instance of:

    if (la instanceof Activity1)
    

    This is the java way to check if something is instance of specific class.

    EDIT And once more the question turns out to be completely different from the initial statement. You are faced with classic form of the Strategy design pattern – many sources on the net, I have linked to Wikipedia. Basically this is a design pattern that allows you to choose the algorithm to use based on the circumstances. Exactly your case. In the article I link, there is a very simple example.

    As for learning basics of design patterns I will definitely recommend the book of Gang of Four. Everybody knows it.

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

Sidebar

Related Questions

I have two classes, and want to include a static instance of one class
I have two classes Order and Items I want a method like this class
I have two applications written in Java that communicate with each other using XML
I have: One MyListActivity class extends ListActivity setContentView(R.layout.mylist) ; Two xml files: mylist.xml and
I have written a ListActivity class that uses a custom ArrayAdapter and Holders. I
have two three tables one for student, second for class, and the third for
I have two arrays of System.Data.DataRow objects which I want to compare. The rows
I have two spreadsheets... when one gets modified in a certain way I want
I have an extended BaseAdapter in a ListActivity: private static class RequestAdapter extends BaseAdapter
I have a lisview that I want to add images to. The examples that

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.