Android lifecycle library ViewModel using dagger 2

You need to implement your own ViewModelProvider.Factory. There is an example app created by Google demonstrating how to connect Dagger 2 with ViewModels. LINK. You need those 5 things: In ViewModel: @Inject public UserViewModel(UserRepository userRepository, RepoRepository repoRepository) { Define annotation: @Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @MapKey @interface ViewModelKey { Class<? extends ViewModel> value(); } In ViewModelModule: @Module … Read more

Jetpack compose – how do I refresh a screen when app returns to foreground

I came up with this: @Composable fun OnLifecycleEvent(onEvent: (owner: LifecycleOwner, event: Lifecycle.Event) -> Unit) { val eventHandler = rememberUpdatedState(onEvent) val lifecycleOwner = rememberUpdatedState(LocalLifecycleOwner.current) DisposableEffect(lifecycleOwner.value) { val lifecycle = lifecycleOwner.value.lifecycle val observer = LifecycleEventObserver { owner, event -> eventHandler.value(owner, event) } lifecycle.addObserver(observer) onDispose { lifecycle.removeObserver(observer) } } } It seems to work just fine. But there … Read more

Display fragment viewpager within a fragment

use AsyncTask to set the adapter for viewPager. It works for me. The asyncTask is to make the original fragment complete it’s transition. and then we proceed with viewPager fragments, basically to avoid recursion. @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mView = inflater.inflate(R.layout.team_card_master, container, false); mViewPager = (ViewPager)mView.findViewById(R.id.team_card_master_view_pager); final Button button … Read more

When Can I First Measure a View?

I didn’t know about ViewTreeObserver.addOnPreDrawListener(), and I tried it in a test project. With your code it would look like this: public void onCreate() { setContentView(R.layout.main); final TextView tv = (TextView)findViewById(R.id.image_test); final LayerDrawable ld = (LayerDrawable)tv.getBackground(); final ViewTreeObserver obs = tv.getViewTreeObserver(); obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { @Override public boolean onPreDraw () { Log.d(TAG, “onPreDraw tv height is … Read more

Why is my onResume being called twice?

if you trying request permissions every time it can cause such problems, just check if you already granted them requestPermissions can cause it: onCreate onStart onResume onPause onResume Use this method ContextCompat.checkSelfPermission(context, permission) to check if permission was granted or not before requesting it This method returns int and you can check it with PackageManager.PERMISSION_GRANTED … Read more

What is lifecycle for RecyclerView adapter?

I think the best way to do that would be to: Keep a subscription reference in the SiteAdapter.ViewHolder unsubscribe the subscription object in onBindViewHolder (it’s called when the ViewHolder is reused) Keep the CompositeSubscription object in your adapter Use the onDetachedFromRecyclerView method of your Adapter to unsubscribe the compositeSubscription Like so: public class SiteAdapter extends … Read more

Why use Fragment#setRetainInstance(boolean)?

How do you as a developer use this Call setRetainInstance(true). I typically do that in onCreateView() or onActivityCreated(), where I use it. and why does it make things easier? It tends to be simpler than onRetainNonConfigurationInstance() for handling the retention of data across configuration changes (e.g., rotating the device from portrait to landscape). Non-retained fragments … Read more

Android Fragment onCreateView vs. onActivityCreated

If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you – for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so. Also accessing the view hierarchy … Read more

Android: Under what circumstances would a Dialog appearing cause onPause() to be called?

onPause() is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause. A dialog (lower-case) does not need to be implemented by a … Read more