2

How can I get the name of the current activity in the base class. Suppose I have a base activity class BaseActivity :

class BaseActivity extends AppCompatActivity
{
    ...
    String getCurrentActivityName() {
            ....
    }
    ...
}

Now I have 2 other classes

class Activity1 extends BaseActivity
{
    ....
}

class Activity2 extends BaseActivity
{
    ....
}

There maybe many more activities extending BaseActivity, for brevity I have used 2 here.

I am not sure how to implement getCurrentActivityName() method in the base class. It should return a String containing the name of the current activity.

EDIT : For some reasons I cannot edit Activity1 and Activity2.

4 Answers4

1

When creating a new activity class you should add classname to the constructor and then get it with getCurrentActivityName method as you were trying

class BaseActivity extends AppCompatActivity
    {
        protected String className;
        public BaseActivity() { this.className = "BaseActivity"; }
        String getCurrentActivityName() {
              return this.className;
        }
        ...
    }

    class Activity1 extends BaseActivity
    {
        protected String className;
        public Activity1() { this.className = "Activity1"; }
    }

    class Activity2 extends BaseActivity
    {   
        protected String className;
        public Activity2() { this.className = "Activity2"; }

    }

Then if you call

BaseActivity baseActivity = new BaseActivity();
baseActivity.getClassname(); // returns "BaseActivity"
Activity1 activity1 = new Activity1();
activity1.getClassname(); // returns "Activity1"

Or if you just want to know the current activity name use . It's as simple as that

this.getClass().getSimpleName()
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
1

Create abstract method getActivityName() in BaseActivity.

Implement getActivityName() in each activity and return Activity.this.getClass().getSimpleName();

Add this

String getCurrentActivityName() {
    return getActivityName();
}
pfm
  • 6,210
  • 4
  • 39
  • 44
1

Apparently this works fine, and I'm surprised the question I previously marked as already having an answer did not mention that within the accepted response.

abstract class A {
    public final String getName() {
         return this.getClass().getName();
    }
}

class B extends A { }

class C extends A { }

This prints the current class name of the implementing instance

public static void main(String[] args) {

    B b = new B();
    C c = new C();

    System.out.println(b.getName());
    System.out.println(c.getName());
}

Output:

com.test.B
com.test.C

You can adapt it to your use-case.

payloc91
  • 3,724
  • 1
  • 17
  • 45
0
public class BaseActivity extends AppCompatActivity {
    ActivityType currentActivityType;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    String getCurrentActivityName(){
        switch (this.currentActivityType){
            case type1:
                return "Activity 1";
            case type2:
                return "Activity 2";
                default:
                    return "Base Activity";
        }

    }
}

enum ActivityType{
    type1,
    type2
}

public class FirstActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        currentActivityType = ActivityType.type1;
    }
}

public class SecondActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        currentActivityType = ActivityType.type2;
    }

}
Fahadsk
  • 1,099
  • 10
  • 24