Blaze Tama
Blaze Tama

Reputation: 10948

Check current subclass in Android

I have an Activity called RootActivity, which will be extended to all of Activities in my apps (or in the other word, it will be the super class and all of my Activities will be its sub class).

Now, lets say I have 3 Activities in my apps : MainActivity, DetailActivity and AboutActivity, and all of them extends RootActivity. How can I check where is the current active Activity, in the RootActivity code?

The simple example is, lets say I want to Toast "This is MainActivity" when I was in MainActivity.

I think I can use if (this instanceof RootActivity) if I put this code in the sub classes (the three Activities above). But, what I'm looking for is how to code it in RootActivity.

Thanks for your time

Upvotes: 1

Views: 201

Answers (2)

David Conrad
David Conrad

Reputation: 16399

You can use this instanceof MainActivity in your RootActivity. It might seem at first glance that this wouldn't work, since it is in code residing in RootActivity, but if your MainActivity class is descended from RootActivity, then it will inherit the code and if the actual run-time type of the object is MainActivity, it will return true.

So, one way to write it would be:

public class RootActivity extends Activity {
    // ...

    public String getToastMessage() {
        if (this instanceof MainActivity) {
            return "This is MainActivity";
        }
        if (this instanceof DetailActivity) {
            return "This is DetailActivity";
        }
        if (this instanceof AboutActivity) {
            return "This is AboutActivity";
        }
    }

    // ...
}

However, code that explicitly tests the class of objects like this is generally considered less elegant than polymorphic code. You could also have a method in the base class which is overridden in each subclass:

public class RootActivity extends Activity {
    // ...

    public String getToastMessage() {
        return "I don't know where I am!";
    }

    // ...
}

public class MainActivity extends RootActivity {
    // ...

    @Override
    public String getToastMessage() {
        return "This is MainActivity";
    }

    // ...
}

And so on for the other classes.

Either approach will work if you want to have behavior vary between the classes. If, on the other hand, you only want to do what is directly asked in your question, which is to have a toast display the name of the class, then a simpler solution is possible since objects in Java know what class they belong to (what is sometimes called Run-Time Type Identification, or RTTI), including the name of their class:

public class RootActivity extends Activity {
    // ...

    public String getToastMessage() {
        return "This is " + this.getClass().getSimpleName();
    }

    // ...
}

Upvotes: 0

Chloe
Chloe

Reputation: 26294

This will give the class name for the current object whichever type it is.

public class RootActivity {

  public String getThisClassName() {
    return this.getClass().getName();
  }
}

See demonstration here: http://ideone.com/5IGRW0

Upvotes: 1

Related Questions