1

Working on an existing project, I found this rather uncommon piece of implementation (to me at least). Since I am not in contact with the previous developer who already left, and before I dismiss this as a case of simply copy and pasting code from another part of our codebase, I would like to ask the SO community about this.

Here's your everyday activity that extends BaseActivity, and the implementation:

class SomeActivity : BaseActivity(R.layout.some_activity) {
    ...
    private fun close() {
        if(backToMain)
            finish()
        } 
        else
            goToMain(this)
    }

    companion object {
        fun goToMain(activity: AppCompatActivity) {
            val intent = Intent(activity, MainActivity::class.java)
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
            activity.startActivity(intent)
            EventBus.getDefault().post(Event(Event.Type.ActivityFinish))
        }
    }
    ...
}

In BaseActivity.kt, there's a subscription to the event which calls finish().

@Subscribe override fun onEvent(event: Event) {
    when (event.type) {
        Event.Type.ActivityFinish -> super.finish()
        else -> {}
    }
}

Now, why would anyone use EventBus to call Activity.finish() from the extended BaseActivity, instead of just calling it then and there (in the above SignActivity)? Even if SomeActivity were actually a fragment, you could simply call getActivity.finish().

*With regards to the intent flags, you could also use Activity.finishAffinity() to finish all activities in the back stack if your min SDK is 16.

Thanks in advance for feeding my curiosity. :D

Aba
  • 2,307
  • 2
  • 18
  • 32
  • it could be a scenario , `sign IN => signUp(on successful signup , kill signIN and take user to Someother screen) => Somesceen` – Pavneet_Singh Aug 03 '17 at 15:38
  • That would actually be a legit situation. However, that implies a communication between two activities, not an activity and it's extended base. – Aba Aug 03 '17 at 17:39

2 Answers2

0

Update - This answer was before mentioning that project is using minimum Api level 16.

I looked into your code and I think developer had very good intention behind that. He wants to clear all Activities from stack. Like here he used FLAG_ACTIVITY_CLEAR_TOP or FLAG_ACTIVITY_NEW_TASK but it requires API level > 11.

By adding subscription in BaseActivity we can make sure that Activity will no longer exist after broadcasting Event.Type.ActivityFinish tag with a use of EventBus. That method will work as a safer approach if you think about logout case where you are going to clear your stack.

Rahul
  • 10,457
  • 4
  • 35
  • 55
  • With regards to the intent flags, you could simply call `Activity.finishAffinity()` and this would, in simple words, call `finish()` for all activities in the back stack. So, I don't think this explains why you would use an EventBus. – Aba Aug 03 '17 at 19:33
  • Again `Activity.finishAffinity` came after API level 16. For more check this https://stackoverflow.com/questions/33497151/activity-finishaffinity-vs-intent-flag-activity-new-task-intent-flag-activit and I don't think there is anything bad to use this approach to kill activities. – Rahul Aug 04 '17 at 06:07
  • Please excuse my ignorance of API level. Yes, you are right. The flags are an OK thing to use (just that my project specifically is minSdk 16). Nonetheless, I don't think it's the reason why EventBus is used because you could simply call `finish()` at the end there. – Aba Aug 04 '17 at 07:05
  • If your project is using minimum 16, which you mentioned now then these subscriptions have no meaning. – Rahul Aug 04 '17 at 07:08
  • Thanks for your feedback @Rahul :D – Aba Aug 04 '17 at 07:09
0

So, here's the deal with this. After some staring on the screen a couple of times and working more with the codebase, I found that the previous developer actually used the goToMain function from other activities (do people really do this? O_o). Nonetheless, since the "Activity" object is still passed, there is no reason that I could think of for using an EventBus in this particular scenario (because you can just call activity.finish()).

In conclusion, the developer probably just adapted this chunk of code from a different part of the app, thinking this is how it works.


What is EventBus?

EventBus is an Android optimized event bus that simplifies communication between Activities, Fragments, Threads, Services, etc.

i.e. Not running a static method to call an activity's own extended functions.

Aba
  • 2,307
  • 2
  • 18
  • 32