Send data from activity to fragment in Android

From Activity you send data with intent as: Bundle bundle = new Bundle(); bundle.putString(“edttext”, “From Activity”); // set Fragmentclass Arguments Fragmentclass fragobj = new Fragmentclass(); fragobj.setArguments(bundle); and in Fragment onCreateView method: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String strtext = getArguments().getString(“edttext”); return inflater.inflate(R.layout.fragment, container, false); }

Understanding Fragment’s setRetainInstance(boolean)

First of all, check out my post on retained Fragments. It might help. Now to answer your questions: Does the fragment also retain its view state, or will this be recreated on configuration change – what exactly is “retained”? Yes, the Fragment‘s state will be retained across the configuration change. Specifically, “retained” means that the … Read more

Android Fragment handle back button press [duplicate]

When you are transitioning between Fragments, call addToBackStack() as part of your FragmentTransaction: FragmentTransaction tx = fragmentManager.beginTransation(); tx.replace( R.id.fragment, new MyFragment() ).addToBackStack( “tag” ).commit(); If you require more detailed control (i.e. when some Fragments are visible, you want to suppress the back key) you can set an OnKeyListener on the parent view of your fragment: … Read more

Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments

UPDATE: onActivityCreated() is deprecated from API Level 28. onCreate(): The onCreate() method in a Fragment is called after the Activity‘s onAttachFragment() but before that Fragment‘s onCreateView(). In this method, you can assign variables, get Intent extras, and anything else that doesn’t involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be … Read more

Call an activity method from a fragment

From fragment to activty: ((YourActivityClassName)getActivity()).yourPublicMethod(); From activity to fragment: FragmentManager fm = getSupportFragmentManager(); //if you added fragment via layout xml YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentById(R.id.your_fragment_id); fragment.yourPublicMethod(); If you added fragment via code and used a tag string when you added your fragment, use findFragmentByTag instead: YourFragmentClass fragment = (YourFragmentClass)fm.findFragmentByTag(“yourTag”);

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are … Read more

OnActivityResult method is deprecated, what is the alternative?

A basic training is available at developer.android.com. Here is an example on how to convert the existing code with the new one: The old way: public void openSomeActivityForResult() { Intent intent = new Intent(this, SomeActivity.class); startActivityForResult(intent, 123); } @Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK && requestCode … Read more

Fragment MyFragment not attached to Activity

I’ve found the very simple answer: isAdded(): Return true if the fragment is currently added to its activity. @Override protected void onPostExecute(Void result){ if(isAdded()){ getResources().getString(R.string.app_name); } } To avoid onPostExecute from being called when the Fragment is not attached to the Activity is to cancel the AsyncTask when pausing or stopping the Fragment. Then isAdded() … Read more

How to add Options Menu to Fragment in Android

Call the super method: Java: @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setHasOptionsMenu(true); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { // TODO Add your menu entries here super.onCreateOptionsMenu(menu, inflater); } Kotlin: override fun void onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) setHasOptionsMenu(true) } override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { // TODO Add your menu entries … Read more

How to handle button clicks using the XML onClick within Fragments

I prefer using the following solution for handling onClick events. This works for Activity and Fragments as well. public class StartFragment extends Fragment implements OnClickListener{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v = inflater.inflate(R.layout.fragment_start, container, false); Button b = (Button) v.findViewById(R.id.StartButton); b.setOnClickListener(this); return v; } @Override public void onClick(View v) … Read more

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