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 – Preserve or delete files created by the application on uninstall

Seems like there have been some developments since 2009 :). From the documentation: If you’re using API Level 8 or greater, use getExternalCacheDir() to open a File that represents the external storage directory where you should save cache files. If the user uninstalls your application, these files will be automatically deleted. However, during the life … Read more

What is the correct order of calling superclass methods in onPause, onStop and onDestroy methods? and Why?

Destroying the instance specific resources first, before destroying superclass resources that the instance specific resources may depend upon makes sense, not the other way round. But the comments suggest otherwise. What am I missing? In my opinion: not a single thing. This answer from Mark (aka CommonsWare on SO) sheds light on the issue: Link … Read more

What is the benefit of using Fragments in Android, rather than Views?

The main reason to use Fragments are for the backstack and lifecycle features. Otherwise, custom views are more light weight and simpler to implement. At first, I actually tried to build a phone/tablet app using custom views. Everything appeared to work across phones AND tablets, even switching from single panel to split panel. Where I … Read more

java.lang.IllegalStateException: Fragment not attached to Activity

This error happens due to the combined effect of two factors: The HTTP request, when complete, invokes either onResponse() or onError() (which work on the main thread) without knowing whether the Activity is still in the foreground or not. If the Activity is gone (the user navigated elsewhere), getActivity() returns null. The Volley Response is … Read more

fragment lifecycle: when “ondestroy” and “ondestroyview” are not called?

Take a look at this question: Why implement onDestroy() if it is not guaranteed to be called? Basically, onDestroy() is only guaranteed to be called if you call finish(). Otherwise, onDestroy() may not be called until the system deems it necessary. You might want to look at putting your “closing” logic in the onPause() or … Read more

One Activity and all other Fragments [closed]

It depends on the app you are creating. I’ve created several apps using both approaches and can’t say one way is always better than the other. The latest app I created I used the single Activity approach and a Facebook style navigation. When selecting items from the navigation list I update a single Fragment container … 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

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