Open OndrejMalek opened 3 years ago
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.
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.
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