Does an Activity extending another activity have 2 separate contexts for super and child classes(Activities) Android?

51 Views Asked by At

I have a template Activity class as

open class AbstractActivity : AppCompatActivity(){
  onCreate()
  ...
}

I will be using this template AbstractActivity for almost every Activity in my project

I have an Activity as :

class MyActivity : AbstractActivity(){


}

On debugging what I have understood is that both have 2 separate contexts instead of one.

When I call finish() inside onActivityResult in AbstractActivity I am closing abstract activity only but not the MyActivity.

From my understanding as per OOPS finish() call here must close the whole MyActivity extending AbstractActivity right but why is only AbstractActivity being finished.

P.S : I am calling finish() from AbstractActivity's onActivityResult method

1

There are 1 best solutions below

2
Muzammal Abdul Ghafoor On

When you call finish() inside onActivityResult() in the AbstractActivity, it should finish the instance of AbstractActivity itself. However, it will not directly affect the instance of MyActivity that inherits from it. If you're seeing the AbstractActivity being finished, but the MyActivity is still running, this is expected behavior as Both activity has different lifecycles on their own.

The finish() method is used to finish the current activity instance, which means it will remove the activity from the activity stack and destroy it. Since you're calling finish() inside AbstractActivity, it will finish that particular instance of AbstractActivity, leaving the MyActivity running if it's still in the foreground.

If you want to finish MyActivity then you have to call finish() explicitly in this activity as well.