2

I am writing followings code for PagerAdapter

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            fragment_edit_delivery_location = new Fragment_edit_delivery_location_Map();
            Bundle arg = new Bundle();
            arg.putString("delivery_to_title_map", "Map Location");
            fragment_edit_delivery_location.setArguments(arg);
            return fragment_edit_delivery_location;


        case 1:
            fragment_edit_delivery_locationHome = new Fragment_edit_delivery_locationHome();
            Bundle arg1 = new Bundle();
            arg1.putString("delivery_to_title_home", "Home");
            fragment_edit_delivery_location.setArguments(arg1);
            return fragment_edit_delivery_location;
}
}

I am getting error as error java.lang.IllegalStateException: Can't change tag of fragment Fragment_edit_delivery_location_Map

Constructor of adapter

public ActionTabAdapter( FragmentManager fm) {
super(fm);
}

I am using ViewPager in MainActivity as :

adapter = new ActionTabAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);

I am trying to get dynamic value From current Fragment and again used in MainActivity to send String value as intent From MainActivity to SecondActivity .....but also not being able to get accurate value from Current Frgment .....Any kind of help is appreciated Thank you

Chintan Bawa
  • 1,376
  • 11
  • 15
  • Possible duplicate of http://stackoverflow.com/questions/16345129/java-lang-illegalstateexception-cant-change-tag-of-fragment – Rohit5k2 Jan 21 '16 at 13:13
  • Think MVC pattern. Build a custom adapter and pass a model object to it that holds the values shown in the fragments. Make the fragments write back changes to the model object. Since the activity was creating the adapter in the first place, it also has access to the model object. *Don't try to pass information "across" views, you will get hurt.* – dhke Jan 21 '16 at 13:22
  • Use static factory methods for your fragments instead of constructing them client side. Its also an easy mechanism to add arguments to as well. – TheSunny Jan 21 '16 at 13:48

1 Answers1

0

use call backs would help you out

1) create an interface

public interface TextCallback {
void getcalltochangeText(String txt);
void getArrayToset(ArrayList<String> data);

}

2) In your fragment class put this way TextCallback textCallback;

textCallback.getcalltochangeText("BREW TEA");

   @Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    if(activity instanceof FragmentCommunicator)
    {


        fc=(FragmentCommunicator)activity;

    }
    else
    {
        throw  new  ClassCastException();
    }

    if(activity instanceof TextCallback)
    {
        textCallback=(TextCallback)activity;
    }
    else
    {
        throw  new  ClassCastException();

    }
}

3) And then in you main activity implement this interface

public class HomeActivity extends FragmentActivity implements View.OnClickListener ,TextCallback



@Override
public void getcalltochangeText(String txt) {
// here you can get passed values from fragment
    setReceivedMsg(txt);
}

// and get the values here that you want

Pratik
  • 456
  • 4
  • 18