I know I am not the first person to rise this question here but unfortunately,As I mentioned in my title ,I got the error when I run the app .But I have imported import android.support.v4.app.Fragment; in my fragments .
From the parent fragment I have a cardview when I clicked on it I need to call another fragment to show the views related to the clicked cardview .
And I have tried lot of solution for the same problem posted in stackoverflow.But nothing is worked for me .
Reminders.java
public class Reminders extends Fragment {
private OnFragmentInteractionListener mListener;
List<reminder> lstreminder;
public Reminders() {
}
public static Reminders newInstance(String param1, String param2) {
Reminders fragment = new Reminders();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment_reminders, container, false);
lstreminder = new ArrayList<>();
lstreminder.add(new reminder("Alarm",R.drawable.alarms));
lstreminder.add(new reminder("Birthday",R.drawable.birthdays));
lstreminder.add(new reminder("Anniversary",R.drawable.anniversaries));
lstreminder.add(new reminder("Buy Gifts",R.drawable.buygift));
lstreminder.add(new reminder("Bill Payments",R.drawable.billpayments));
lstreminder.add(new reminder("Call / SMS",R.drawable.call));
lstreminder.add(new reminder("Recharges",R.drawable.recharges));
lstreminder.add(new reminder("Meetings",R.drawable.meetings));
lstreminder.add(new reminder("Insurance",R.drawable.insurances));
lstreminder.add(new reminder("Exams",R.drawable.exams));
lstreminder.add(new reminder("Holidays",R.drawable.holidays));
lstreminder.add(new reminder("Medical",R.drawable.medical));
lstreminder.add(new reminder("Purchase",R.drawable.purchase));
lstreminder.add(new reminder("Travel",R.drawable.travel));
lstreminder.add(new reminder("Shows",R.drawable.shows));
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.mRecyclerView_id);
final RecyclerviewAdapter recyclerviewAdapter = new RecyclerviewAdapter(getActivity(),lstreminder);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(),3));
recyclerView.setAdapter(recyclerviewAdapter);
recyclerviewAdapter.setOnItemClickListner(new RecyclerviewAdapter.onItemClickListner() {
#Override
public void onClick(reminder str) {
if(str.getmTitle().equals("Alarm")){
Fragment fragment = new Alarm();
getFragmentManager().beginTransaction()
.replace(R.id.fragmrnt_alarm, fragment, fragment.getClass().getSimpleName()).addToBackStack(null).commit();
}
recyclerviewAdapter.notifyDataSetChanged();
}
});
return rootView;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Alarm.java
public class Alarm extends Fragment {
private OnFragmentInteractionListener mListener;
public Alarm() {
}
public static Alarm newInstance(String param1, String param2) {
Alarm fragment = new Alarm();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_alarm, container, false);
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
You need to make the parent activity implement OnFragmentInteractionListener and override the methods provided by that interface.
class MyActivity implements OnFragmentInteractionListener
This is where your code is crashing, your activity probably does not implement the metioned interface and because of that the crash happens when the fragment is attached:
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
Related
I have 3 fragments where 1 fragment makes changes to the other 2.
The problem is: When I rotate the screen background color and places of fragments 2 and 3 are restored.
I want to keep them without modifying AndroidManifest.xml.
Is it best to use onSaveInstanceState or setRetainInstance or both?
I found some other answers about retaining fragments but none of them were beneficial for my case.
FirstFragment
public class FirstFragment extends Fragment
{
private OnFragmentInteractionListener mListener;
private ChangeColorListener mCallBack;
private boolean clickCheck = false;
public FirstFragment() { }
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
Button buttonOk = v.findViewById(R.id.button_ok);
Button btn = v.findViewById(R.id.button_replace);
buttonOk.setOnClickListener(v1 -> mCallBack.changeColor(R.color.black));
btn.setOnClickListener(v12 -> {
clickCheck = !clickCheck;
mListener.changeFragment(clickCheck);
});
return v;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState!=null) {
mCallBack =(MainActivity)getActivity();
mListener = (MainActivity)getActivity();
}
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
if (context instanceof ChangeColorListener) {
mCallBack = (ChangeColorListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement ChangeColorListener");
}
try {
mListener = (OnFragmentInteractionListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
SecondFragment
public class SecondFragment extends Fragment
{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_second, container, false);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
public void change(int color){
getView().setBackgroundColor(color);
}
}
ThirdFragment
public class ThirdFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_third, container, false);
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
}
#Override
public void onDetach() {
super.onDetach();
}
public void change(int color){
getView().setBackgroundColor(color);
}
}
setRetainInstance() is deprecated. ViewModel is recommended for retaining data across configuration changes.
In onAttach() method communicator =(Communicator) activity returns null. When i click on the Image View it shows the value of communicatior is null.
I have check but not able to resolve the problem. Here in the fragment when click on the image view some data should transfer to the activity. But the reference variable of Interface Commnunicator communicator gets the null value in onAttch()
public class SendFragment extends Fragment {
ImageView imgContact;
TextView tvContactName;
String username;
Communicator communicator;
public interface Communicator {
public void sendDataToActivity(String s);
}
public static final String TAG = SendFragment.class.getSimpleName();
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Communicator) {
communicator = (Communicator) context;
} else {
throw new IllegalArgumentException(
"Must implement " + TAG + ".Communicator on caller Activity");
}
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_send, container, false);
imgContact = (ImageView) view.findViewById(R.id.contactbook);
tvContactName = (TextView) view.findViewById(R.id.tvcontactname);
if(!TextUtils.isEmpty(username)) {
username = getArguments().getString("username").toString();
tvContactName.setText(username);
}
listeners();
return view;
}
private void listeners() {
imgContact.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
communicator.sendDataToActivity(Constants.selectUserName);
Intent intent = new Intent(getActivity(), AllContactsListActivity.class);
startActivity(intent);
}
});
}
}
Now the below is my Code of the activity..
public class AllContactsListActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, SendFragment.Communicator,RecyclerViewAdapter.OnItemClick {
Context context;
RecyclerView recyclerView;
RecyclerView.Adapter recyclerViewAdapter;
RecyclerView.LayoutManager recylerViewLayoutManager;
ImageButton btnSliding_common;
DrawerLayout drawer;
NavigationView navigationView;
String sendFragmentStringFlag;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_contacts_list);
setIds();
listeners();
}
private void listeners() {
recyclerViewAdapter = new RecyclerViewAdapter(AllContactsListActivity.this, subjects, emails, address,this);
recyclerView.setAdapter(recyclerViewAdapter);
RecyclerView.ItemDecoration itemDecoration =
new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
recyclerView.addItemDecoration(itemDecoration);
btnSliding_common.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
drawer.openDrawer(GravityCompat.END);
}
});
}
private void setIds() {
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
btnSliding_common = (ImageButton) findViewById(R.id.btnSliding_contact);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout_contact);
navigationView = (NavigationView) findViewById(R.id.nav_view_contact);
recylerViewLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(recylerViewLayoutManager);
}
#Override
public void sendDataToActivity(String sendMoneyflag) {
sendFragmentStringFlag=sendMoneyflag;
}
#Override
public void onClick(String username) {
if(!TextUtils.isEmpty(sendFragmentStringFlag)) {
if (sendFragmentStringFlag.equals(Constants.selectUserName)) {
Toast.makeText(this, "You clicked " + username, Toast.LENGTH_SHORT).show();
Bundle bundle = new Bundle();
bundle.putString("username", username);
SendFragment sendFragment = new SendFragment();
sendFragment.setArguments(bundle);
finish();
}
}else{
Toast.makeText(this, "USER NAME" + username, Toast.LENGTH_SHORT).show();
}
}
}
The problem probably because the host activity didn't implement the Fragment interface.
You're trying to attach the interface with the following method:
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
communicator = (Communicator) activity;
} catch (Exception e){}
}
But you didn't handle the Exception correctly. So, you don't know when the interface is set correctly. And you also override onAttach(Activity activity) which is now deprecated.
You need to change the code as following:
public static final String TAG = YourFragment.class.getSimpleName();
...
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof Communicator) {
communicator = (Communicator) context;
} else {
throw new IllegalArgumentException(
"Must implement " + TAG + ".Communicator on caller Activity");
}
}
It will throw error if your host activity didn't implement the interface.
You also need to attach the fragment to your activity by using FragmentTransaction. Something like this:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
SendFragment sendFragment = new SendFragment();
fragmentTransaction.add(R.id.fragment_container, sendFragment);
fragmentTransaction.commit();
I spend time to search an answer to my problem without success.
When i rotating my fragments are recreated inside viewpager adapter but not displayed, i don't no why ?
I have tried FragmentPagerStateAdapter instead of FragmentPagerAdapter but it doesn't work.
My activity code :
public class BookingsActivity extends BaseActivity implements ActivityCompat.OnRequestPermissionsResultCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener,LoginDialogFragment.LoginDialogFragmentListener, BookingsFragment.OnFragmentInteractionListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bookings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
.....
setupViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(BookingsFragment.newInstance(), "A venir");
adapter.addFragment(BookingsFragment.newInstance(), "Terminées");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentStatePagerAdapter {
private final List<BookingsFragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public BookingsFragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(BookingsFragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
My fragment code :
public class BookingsFragment extends Fragment {
private ArrayList<Bookings> mBookings = new ArrayList<>();
private BookingCardAdapter adapter;
private RecyclerView recyclerView;
private LinearLayoutManager mLayoutManager;
private OnFragmentInteractionListener mListener;
private Location mLastLocation;
public BookingsFragment() {
// Required empty public constructor
}
public static BookingsFragment newInstance() {
BookingsFragment fragment = new BookingsFragment();
// Supply index input as an argument.
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_bookings, container, false);
// Inflate the layout for this fragment
recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
mLayoutManager = new LinearLayoutManager(this.getContext());
recyclerView.setLayoutManager(mLayoutManager);
adapter = new BookingCardAdapter(mBookings, this.getContext(), new BookingCardAdapter.BookingCardAdapterSubmitListener() {
#Override
public void onBookingClick(Bookings booking, int state) {
handleBookingClick(booking,state);
}
});
recyclerView.setAdapter(adapter);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void setBookings(ArrayList<Bookings> bookings, int page) {
if (bookings != null && bookings.size() > 0) {
if (adapter != null) {
if (page == 1) {
mBookings.clear();
}
for (Bookings b : bookings) {
mBookings.add(b);
}
adapter.notifyDataSetChanged();
}
}
}
public void setLocation(Location location) {
if (location!=null) {
mLastLocation=location;
}
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
I follow the instructions on the website : http://jakewharton.github.io/butterknife/
error : java.lang.RuntimeException: Unable to start activity ComponentInfo. java.lang.RuntimeException: Unable to bind views for com.project.myapp.OneFragment.
I try to remove #Bind(R.id.btnNext) Button btnNext; and run no error.
public class OneFragment extends Fragment {
#Bind(R.id.btnNext) Button btnNext;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public OneFragment() {
// Required empty public constructor
}
public static OneFragment newInstance(String param1, String param2) {
OneFragment fragment = new OneFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
ButterKnife.bind(this, view);
return view;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onFragmentInteraction(Uri uri);
}
}
Replace this:
ButterKnife.bind(this, view);
with
ButterKnife.bind(getActivity(), view);
i'am tryning to update my view pager indicator from my fragment.
My problem is the project is structured like this:
MainActivity -> FragmentA -> FragmentStatePagerAdapter -> FragmentProduct
I've tried implementing an interface in the adapter but the adapter isn't able to catch the event, only the mainActivity is catching the event.
So the only thing i want it's to update view pager (FragmentStatePagerAdapter) from the fragment.
MainActivity:
public class ProductDetailActivity extends ActivityBase implements ProductFragment.OnFragmentInteractionListener, TabIndicatorListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
#Override
public void updateTabIndicator(ProductFragment productFragment) {
} }
**FragmentA - Set adapter etc etc **
public class ProductFragmentDetails extends FragmentBase {
private NavigationHelper navHelper = NavigationHelper.getInstance();
ViewPager viewPager;
PagerAdapter adapter;
TabPageIndicator indicator;
List<ProductPagerAdapter.RegisterValue> prices;
private FragmentManager fm;
ProductPagerAdapter pagerUpdater;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
createDummyPrices();
View mainView = inflater.inflate(R.layout.fragment_product_details, container);
viewPager = (ViewPager) mainView.findViewById(R.id.pager);
viewPager.setVisibility(View.VISIBLE);
indicator = (TabPageIndicator) mainView.findViewById(R.id.indicator);
fm = getActivity().getSupportFragmentManager();
adapter = new ProductPagerAdapter(fm, getActivity().getApplicationContext(), indicator, prices);
viewPager.setAdapter(adapter);
indicator.setViewPager(viewPager);
indicator.setVisibility(View.VISIBLE);
return mainView;
}}
FragmentStateAdapter
public class ProductPagerAdapter extends FragmentStatePagerAdapter implements IconPagerAdapter, TabIndicatorListener {
... init vars etc etc
#Override
public void startUpdate(ViewGroup container) {
super.startUpdate(container);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
#Override
public CharSequence getPageTitle(int position) {
return Integer.toString(position + 1);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
#Override
public void setPrimaryItem(ViewGroup container, int position, Object object) { super.setPrimaryItem(container, position, object);
}
#Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return super.isViewFromObject(view, object);
}
#Override
public Parcelable saveState() {
return super.saveState();
}
#Override
public void restoreState(Parcelable state, ClassLoader loader) {
super.restoreState(state, loader);
}
/**
* Get icon representing the page at {#code index} in the adapter.
*
* #param index
*/
#Override
public int getIconResId(int index) {
return prices.get(index).icon;
}
#Override
public int getCount() {
return prices.size(); //The number of items in the pagerAdapter must be equal to number of products
}
// Instantiate the fragment to display for that page
#Override
public Fragment getItem(int position) {
String productPrice = Double.toString(prices.get(position).price);
return ProductFragment.newInstance(productPrice, "OBS");
}
#Override
public void updateTabIndicator(ProductFragment productFragment) {
}
FragmentProduct
public class ProductFragment extends FragmentBase {
init some vars and fields....
Context context;
TabIndicatorListener tabIndicatorListener;
public static ProductFragment newInstance(String price, String observations) {
ProductFragment fragment = new ProductFragment();
Bundle args = new Bundle();
args.putString(ARG_PRICE, price);
args.putString(ARG_OBSERVATIONS, observations);
fragment.setArguments(args);
return fragment;
}
public ProductFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity();
if (getArguments() != null) {
mParamPrice = getArguments().getString(ARG_PRICE);
mParamObservation = getArguments().getString(ARG_OBSERVATIONS);
}
try {
tabIndicatorListener = (TabIndicatorListener) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException(getActivity().toString()
+ " must implement TabIndicatorListener");
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View productView = inflater.inflate(R.layout.fragment_product, container, false);
initViews(productView); //Its initialized the views..
btnAbsent.setOnClickListener(clickOnBtnAbsent);
btnValidate.setOnClickListener(clickOnBtnValidate);
editTxtPrice.setText(String.valueOf(mParamPrice));
return productView;
}
/**
* #param productView
*/
private void initViews(View productView) {
txtViewProductName = (TextView) productView.findViewById(R.id.txtViewNameProduct);
txtViewPrice = (TextView) productView.findViewById(R.id.txtViewPrice);
}
#Override
protected int getActionBarMenuRes() {
return 0;
}
#Override
protected int getMainLayoutRes() {
return 0;
}
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
public View.OnClickListener clickOnBtnAbsent = new View.OnClickListener() {
#Override
public void onClick(View v) {
tabIndicatorListener.updateTabIndicator(ProductFragment.this);
//Here is where i want to update my view pager indicator
}
};
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onFragmentInteraction(Uri uri);
}
First of all I'm surprised that you didn't encounter problems due to the FragmentManager you used. Instead of :
fm = getActivity().getSupportFragmentManager();
you should use the dedicated FragmentManager for nested fragments:
fm = getChildFragmentManager();
Secondly, related to the listener target you can use one of the methods of the fragment class, getParentFragment(). This method returns, for nested fragments, the container fragment:
// in ProductFragment
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mListener = ((ProductFragmentDetails) getParentFragment()).getListener();
}
In ProductFragmentDetails you'll have the method:
public (OnFragmentInteractionListener) getListener() {
return (OnFragmentInteractionListener) adapter;
}