Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Hilt] cannot override init() in BaseActivity #2456

Closed
tekinumut opened this issue Mar 5, 2021 · 1 comment · Fixed by #2458
Closed

[Hilt] cannot override init() in BaseActivity #2456

tekinumut opened this issue Mar 5, 2021 · 1 comment · Fixed by #2458

Comments

@tekinumut
Copy link

tekinumut commented Mar 5, 2021

I started getting this error after updating Hilt from 2.32-alpha to 2.33-beta. The error occurs because of i use function in BaseActivity named init

I think the problem is Hilt is already using a function named initt and this function conflicts with my function. The problem is fixed after changing the name of the init function. But i don't wanna change it's name.

Note: This problem is not happening in Fragments.

Base Activity.

abstract class BaseActivity<DB : ViewDataBinding, VM : BaseViewModel> : AppCompatActivity() {

   @get:LayoutRes
   protected abstract val layoutResourceId: Int
   protected abstract val classTypeOfViewModel: Class<VM>
   lateinit var binding: DB
   lateinit var viewModel: VM

   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      viewModel = ViewModelProvider(this).get(classTypeOfViewModel)
      binding = DataBindingUtil.setContentView(this, layoutResourceId)
      binding.lifecycleOwner = this

      init()
...
   }

   open fun init() {}
...

Activity which uses Hilt and extends Base Activity.

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding, MainViewModel>() {
   override val layoutResourceId: Int = R.layout.activity_main
   override val classTypeOfViewModel: Class<MainViewModel> = MainViewModel::class.java

   override fun init() {
      binding.mainVm = viewModel
      binding.navView.setupWithNavController(navController)
   }
...

This is the class which gives the error

public abstract class Hilt_MainActivity<DB extends ViewDataBinding, VM extends BaseViewModel> extends BaseActivity<DB, VM> implements GeneratedComponentManagerHolder {
  private volatile ActivityComponentManager componentManager;

  private final Object componentManagerLock = new Object();

  private boolean injected = false;

  Hilt_MainActivity() {
    super();
    init();
  }

// I'M GETTING THE ERROR IN THIS FUNCTION
  private void init() {
    addOnContextAvailableListener(new OnContextAvailableListener() {
      @Override
      public void onContextAvailable(Context context) {
        inject();
      }
    });
  }
@bcorso
Copy link

bcorso commented Mar 5, 2021

Hi @tekinumut,

Sorry about that! Yeah, this is related to the Activity injection timing changes mentioned in the 2.33 release notes, so it only affects Hilt activities.

I think you're right, we should rename our init method something like hiltInit to avoid conflicts with common user method names.

copybara-service bot pushed a commit that referenced this issue Mar 5, 2021
…r method of same name.

Fixes #2456

RELNOTES="Fix #2456: Rename init() method in generated Activity to avoid conflict with user method of same name."
PiperOrigin-RevId: 361156462
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants