Difference between Activity and FragmentActivity

A FragmentActivity is a subclass of Activity that was built for the Android Support Package. The FragmentActivity class adds a couple new methods to ensure compatibility with older versions of Android, but other than that, there really isn’t much of a difference between the two. Just make sure you change all calls to getLoaderManager() and … Read more

Receive result from DialogFragment

Use myDialogFragment.setTargetFragment(this, MY_REQUEST_CODE) from the place where you show the dialog, and then when your dialog is finished, from it you can call getTargetFragment().onActivityResult(getTargetRequestCode(), …), and implement onActivityResult() in the containing fragment. It seems like an abuse of onActivityResult(), especially as it doesn’t involve activities at all. But I’ve seen it recommended by official google … Read more

Retrieve a Fragment from a ViewPager

The main answer relies on a name being generated by the framework. If that ever changes, then it will no longer work. What about this solution, overriding instantiateItem() and destroyItem() of your Fragment(State)PagerAdapter: public class MyPagerAdapter extends FragmentStatePagerAdapter { SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>(); public MyPagerAdapter(FragmentManager fm) { super(fm); } @Override public int getCount() { … Read more

How to use data-binding with Fragment

The data binding implementation must be in the onCreateView method of the fragment, delete any data Binding that exist in your OnCreate method, your onCreateView should look like this: public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { MartianDataBinding binding = DataBindingUtil.inflate( inflater, R.layout.martian_data, container, false); View view = binding.getRoot(); //here data must … Read more

How do I add a Fragment to an Activity with a programmatically created content view

It turns out there’s more than one problem with that code. A fragment cannot be declared that way, inside the same java file as the activity but not as a public inner class. The framework expects the fragment’s constructor (with no parameters) to be public and visible. Moving the fragment into the Activity as an … Read more

How to clear navigation Stack after navigating to another fragment in Android

First, add attributes app:popUpTo=’your_nav_graph_id’ and app:popUpToInclusive=”true” to the action tag. <fragment android:id=”@+id/signInFragment” android:name=”com.glee.incog2.android.fragment.SignInFragment” android:label=”fragment_sign_in” tools:layout=”@layout/fragment_sign_in” > <action android:id=”@+id/action_signInFragment_to_usersFragment” app:destination=”@id/usersFragment” app:launchSingleTop=”true” app:popUpTo=”@+id/main_nav_graph” app:popUpToInclusive=”true” /> </fragment> Second, navigate to the destination, using above action as parameter. findNavController(fragment).navigate( SignInFragmentDirections.actionSignInFragmentToUserNameFragment()) See the docs for more information. NOTE: If you navigate using method navigate(@IdRes int resId), you won’t get the … Read more

Android DialogFragment vs Dialog

Yes, use DialogFragment and in onCreateDialog you can simply use an AlertDialog builder anyway to create a simple AlertDialog with Yes/No confirmation buttons. Not very much code at all. With regards handling events in your fragment there would be various ways of doing it but I simply define a message Handler in my Fragment, pass … Read more

Clear back stack using fragments

I posted something similar here From Joachim’s answer, from Dianne Hackborn: http://groups.google.com/group/android-developers/browse_thread/thread/d2a5c203dad6ec42 I ended up just using: FragmentManager fm = getActivity().getSupportFragmentManager(); for(int i = 0; i < fm.getBackStackEntryCount(); ++i) { fm.popBackStack(); } But could equally have used something like: ((AppCompatActivity)getContext()).getSupportFragmentManager().popBackStack(String name, FragmentManager.POP_BACK_STACK_INCLUSIVE) Which will pop all states up to the named one. You can then … Read more

Replace Fragment inside a ViewPager

There is another solution that does not need modifying source code of ViewPager and FragmentStatePagerAdapter, and it works with the FragmentPagerAdapter base class used by the author. I’d like to start by answering the author’s question about which ID he should use; it is ID of the container, i.e. ID of the view pager itself. … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)