Posts

Showing posts with the label Android Fragments

Android Refresh A Fragment List From Its Parent Activity

Answer : You can easily achieve this using INTERFACE MainActivity.java public class MainActivity extends Activity { public FragmentRefreshListener getFragmentRefreshListener() { return fragmentRefreshListener; } public void setFragmentRefreshListener(FragmentRefreshListener fragmentRefreshListener) { this.fragmentRefreshListener = fragmentRefreshListener; } private FragmentRefreshListener fragmentRefreshListener; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button b = (Button)findViewById(R.id.btnRefreshFragment); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(getFragmentRefreshListener()!=null){ getFragmentRefreshListener().onRefresh(); } } }); } publi...

Android - GetIntent() From A Fragment

Answer : You can use a getIntent() with Fragments but you need to call getActivity() first. Something like getActivity().getIntent().getExtras().getString("image") could work. It's not that you can't pass data, it's that you don't want to. From the Fragment documentation: Often you will want one Fragment to communicate with another, for example to change the content based on a user event. All Fragment-to-Fragment communication is done through the associated Activity. Two Fragments should never communicate directly. If you take a look at the Fragment documentation, it should walk you through how to do this. If you want get intent data, you have to call Fragment's method getArguments() , which returns Bundle with extras.