Access Fragment in Activity?

You could do the following with Otto event bus: public class UpdateListEvent { private int fragmentState; public UpdateListEvent(int fragmentState) { this.fragmentState = fragmentState; } } public class MainActivity extends ActionBarActivity { … public void updatelist() { SingletonBus.INSTANCE.getBus().post(new UpdateListEvent(fragmentState)); } } public class FragmentA extends Fragment { @Override public void onResume() { super.onResume(); SingletonBus.INSTANCE.getBus().register(this); } @Override … Read more

FragmentTabHost with horizontal scroll

Based on my experience, the FragmentTabHost doesn’t care much about the xml definitions, things must be made programmatically. I got it working by leaving out the HorizontalScrollView from the xml and adding it in my onCreate for the FragmentActivity. The xml: <android.support.v4.app.FragmentTabHost xmlns:android=”http://schemas.android.com/apk/res/android” xmlns:tools=”http://schemas.android.com/tools” android:id=”@android:id/tabhost” android:layout_width=”match_parent” android:layout_height=”match_parent” > <LinearLayout android:layout_width=”match_parent” android:layout_height=”match_parent” android:orientation=”vertical” > <TabWidget android:id=”@android:id/tabs” … Read more

How to pass a value from one Fragment to another in Android?

You can do something like below, String cid=id.getText().toString(); Fragment fr=new friendfragment(); FragmentManager fm=getFragmentManager(); android.app.FragmentTransaction ft=fm.beginTransaction(); Bundle args = new Bundle(); args.putString(“CID”, cid); fr.setArguments(args); ft.replace(R.id.content_frame, fr); ft.commit(); To receive the data do the following, @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { String strtext = getArguments().getString(“CID”); return inflater.inflate(R.layout.fragment, container, false); }

FragmentTransaction hide/show doesn’t work sometimes

You need to reuse the same instance of a fragment that you wanted to hide or show. private fun replaceFragment(fragment: Fragment) { supportFragmentManager.beginTransaction().apply { if (fragment.isAdded) { show(fragment) } else { add(R.id.fmFragmentContainer, fragment) } supportFragmentManager.fragments.forEach { if (it != fragment && it.isAdded) { hide(it) } } }.commit() }

implement AsyncTask in Fragment android

There is easy step to crate asyntask within fragment in your fragment place code on activityCreateview new MyAsyncTask(getActivity(), mListView).execute(“”); getActivity() is method to communicate with fragment and activity in asynstak on post context.mListView.setArrayAdapter(………..) here is public class SalesFragment extends Fragment { GridView gridView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View gv … Read more

onActivityResult not call in the Fragment

The reason why this doesn’t work is because you are calling startActivityForResult() from within a nested fragment. Android is smart enough to route the result back to an Activity and even a Fragment, but not to a nested Fragment hence why you don’t get the callback. (more information to why that doesn’t work here or … Read more

Struggling with Youtube Player Support Fragment

I ran into this problem before and I believe the issue stemmed from trying to inflate the YouTubePlayerSupportFragment layout. I solved my issue by creating a fragment like this: public class PlayerYouTubeFrag extends YouTubePlayerSupportFragment { private String currentVideoID = “video_id”; private YouTubePlayer activePlayer; public static PlayerYouTubeFrag newInstance(String url) { PlayerYouTubeFrag playerYouTubeFrag = new PlayerYouTubeFrag(); Bundle … Read more

Retain Fragment state between Activities

Since API Level 13 (HONEYCOMB_MR2, June 2011), you can save and restore the state of a fragment across activities. To save the state, use FragmentManager.saveFragmentInstanceState(), providing a reference to the Fragment whose state you wish to save. The Fragment must be attached at the time you attempt to save its state. To restore the state, … Read more