简体   繁体   中英

How to see child Activity from its ancestor, in Android Java Programming?

I know this question does not make great sense, but somehow I think that its not impossible.

I have defined a "base" class, where all the other activities extend it. Like this:

public class ExtendedActivity extends Activity 
{
}

public class Activity1 extends ExtendedActivity
{
}

public class Activity2 extends ExtendedActivity
{
}

So I need to call a method in ExtendedActivity 's onCreate method, only if the current Activity is not Activity1

How can I manage that ? Thanks for any help.

Lets say you have classes as follows:

public class ExtendedActivity extends Activity 
{
    protected void method1() {
    }
}

public class Activity1 extends ExtendedActivity
{

}

public class Activity2 extends ExtendedActivity
{
}

You can call method1() only if currentActivity is NOT Activity1 as follows:

 Activity currentActivity = this;   
 if(!(currentActivity instanceof Activity1)) {
        currentActivity.method1();
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM