How to Get the Fragment Instance from the Fragmentactivity

Get Fragment instance in Activity

Try this

getSupportFragmentManager().beginTransaction()
.add(R.id.container,new MyFragment(),"MyFragment").commit();

for get the fragment

MyFragment frag = ((MyFragment) getSupportFragmentManager().findFragmentByTag("MyFragment"));

How to get the fragment instance from the FragmentActivity?

You can use use findFragmentById in FragmentManager.

Since you are using the Support library (you are extending FragmentActivity) you can use:

getSupportFragmentManager().findFragmentById(R.id.pageview)

If you are not using the support library (so you are on Honeycomb+ and you don't want to use the support library):

getFragmentManager().findFragmentById(R.id.pageview)

Please consider that using the support library is recommended even on Honeycomb+.

How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

Since you only have 3 pages (3 fragments), I recommend that you use the FragmentPagerAdapter rather than the FragmentStatePagerAdapter. Then, you can use findFragmentByTag to find your fragments - read more about that in This SO question.

Get fragment activity outside of Fragment.OnCreateView()

I haven't tested this, but it seems to me that it would work.

Add a Context parameter to the constructor for both TabsPagerAdapter and MyFragment.

Something like this:

TabsPagerAdapter.java:

public class TabsPagerAdapter extends FragmentPagerAdapter {

private Context mCtx;

public TabsPagerAdapter(FragmentManager fragmentManager, Context context) {
super(fragmentManager);
mCtx = context;
}

@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new FirstFragment(mCtx);
case 1:
return new SecondFragment(mCtx);
case 2:
return new HelpFragment(mCtx);
}

return null;
}

@Override
public int getCount() {
return 3; // get item count - equal to number of tabs
}

public static void setTabColor(TabHost tabhost) {
for(int i=0;i<tabhost.getTabWidget().getChildCount();i++) {
tabhost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#FF0000")); //unselected
}
tabhost.getTabWidget().getChildAt(tabhost.getCurrentTab()).setBackgroundColor(Color.parseColor("#0000FF")); // selected
}

}

MainActivity.java:

  mAdapter = new TabsPagerAdapter(this.getSupportFragmentManager(), this);

MyFragment.java (do this in FirstFragment, SecondFragment, and HelpFragment):

public static class MyFragment extends Fragment {

private Context fragmentActivity ;

public MyFragment(Context context){
fragmentActivity = context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//.........
return rootView;
}

@Override
public void onLoadAndStoreComplete() {

//do something with fragmentActivity

}
}

Get view of Fragment from FragmentActivity

If you want to access your component and set some data, I suggest you to make a method inside the fragment like this:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class DetailFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}

public void setSettings(){
Button button = (Button) getView().findViewById(R.id.my_button);
button.setOnClickListener(new MyClickListener());
}
}

How can I access getSupportFragmentManager() in a fragment?

You can directly call

getParentFragmentManager()  

to get the fragment manager. Note that getFragmentManager() also works but has been marked as deprecated.



Related Topics



Leave a reply



Submit