Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Migration from CompositeAndroid to official Android support for composition - question #73

Open
OndrejMalek opened this issue Feb 8, 2021 · 2 comments

Comments

@OndrejMalek
Copy link

Hi, as of AndroidX supports lifecycle and Activity result listening (currently rc 'androidx.activity:activity:1.2.0-rc01') it seemed that this library could be replaced. But so far difference is that lifecycle listeners can be add only before onResume() what requires quite some changes to old code bases, when you want for example catch onActivityResult() from dialog (eg. open android file chooser). And dialog is open from menu, which is created by onCreateOptionsMenu().

Am I missing something or is there possibility do add lifecycle listener after onResume?

ComponentActivity
ActivityResultCaller#registerForActivityResult

@passsy
Copy link
Owner

passsy commented Feb 8, 2021

Honestly, I'm not sure I can help you here. I wrote my last Android code two years ago and never worked with androidx.activity.

Why exactly can't you add the listener before onResume? Register all "modules/plugins" in onCreate and then wait for the callbacks from the lifecycle.

@OndrejMalek
Copy link
Author

OndrejMalek commented Feb 10, 2021

Thx for reaction. If you call 'ActivityResultCaller#registerForActivityResult' after 'onResume' android framework throws Exception.
I can see there is obvious reason that it must be done this way to create listeners again if activity was destroyed due to rotation or whatever.
In my particular case I am working on app which is about 9 years old. I am creating options menu in onCreateOptionsMenu so to use Android Lifecycle listening I would have refactor code to use composition of menus ie. this:

onCreate(){
navigationView.setNavigationItemSelectedListener(
  new NavigationView.OnNavigationItemSelectedListener() {
      @Override
      public boolean onNavigationItemSelected(MenuItem menuItem) {
          drawerLayout.closeDrawers();

          switch (menuItem.getItemId()) {
              case R.id.login:
                  showLoginDialog(MainActivity.this, appModule);
                  break;
             ...
             ...

to sort of (pseudo code):

onCreate(){
setupMenuItem(this,menu);
...
void setupMenuItem(Activity activity, Menu menu){
    listener = new ActivityResultListener()
    activity.registerForActivityResult(listener)
    menu.add(R.id.login, () - {showLoginDialog(MainActivity.this, appModule,listener);})
}

My point is that compared to CompositeAndroid you have to do more work. But maybe I just want a cheese for my wine.

Btw. Thanks for this library.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants