3

i wanna pass a string to all fragment(child) from fragment activity (main), may be this picture can explain what exactly what i want to do

https://dl.dropboxusercontent.com/u/57465028/SC20140205-163325.png

so, from above picture...i wanna pass a string from edittext by press a button to all activity in viewpager....how could i do that?

i tried to follow this code https://stackoverflow.com/a/12739968/2003393 but it can't solved my problem..

please help me...i'm stuck

thank in advance.

here is my code from fragment activity (MainActivity)

public class Swipe_Menu extends FragmentActivity {

//String KeyWord;


//private static final String KEYWORD = "keyword";

private ViewPager _mViewPager;
private ViewPagerAdapter _adapter;
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.swipe_menu_image);


    Button Back = (Button)findViewById(R.id.account);
    ImageButton Search = (ImageButton)findViewById(R.id.search);
    EditText Keyword = (EditText)findViewById(R.id.keyword);

    final String KeyWord = Keyword.getText().toString(); 


    /**
     * Back button click event
     * */
    Back.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            finish();
        }
    });



    setUpView();
    setTab();
}

protected void sendValueToFragments(String value) {
    // it has to be the same name as in the fragment
    Intent intent = new Intent("my_package.action.UI_UPDATE");
    intent.putExtra("UI_KEY", KeyWord  );
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

and here is my fragment (Child Activity)

public class Store_Swipe extends Fragment {

public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";

String KeyWord;
private TextView kata_keyword;

protected BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if(ACTION_INTENT.equals(intent.getAction())) {
            String value = intent.getStringExtra("UI_KEY");
            updateUIOnReceiverValue(value);
        }
    }
};

private void updateUIOnReceiverValue(String value) {
    // you probably want this:
    KeyWord = value;
}

public static Fragment newInstance(Context context) {
    Store_Swipe f = new Store_Swipe();
    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    IntentFilter filter = new IntentFilter(ACTION_INTENT);
    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
}

@Override
public void onDestroy() {
    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
    super.onDestroy();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /*Bundle bundle = this.getArguments();
    KeyWord = bundle.getString("keyword");*/
    View view = inflater.inflate(R.layout.store_swipe, container, false);
    init(view);

    return view;

}

void init(View view) {
    kata_keyword = (TextView) view.findViewById(R.id.keyword);
    //ImageView image = (ImageView) view.findViewById(R.id.image_error);
    kata_keyword.setText(KeyWord);
}

}

Community
  • 1
  • 1
Arsyah
  • 79
  • 3
  • 11

2 Answers2

4

You don't have access directly to your fragments that reside in ViewPager so you can't reference them directly.

What I am doing in these cases is send a broadcast message from Activity to Fragments. For this reason register a BroadcatReceiver in the fragment (either in onCreate or onCreateView - your decision)m, set a custom action for that receiver (ex. "my_package.actions.internal.BROADCAST_ACTION"), don't forget to unregister the receiver from complementary method.

When you want to send a message from activity, create an intent with above mentioned action, add the string in intent extra and send the broadcast.

In your receiver's onReceive method (within the fragment), get the String from intent paramter and there you have the string.

Makes sense?

EDIT: To provide some code, below are the changes that I would make for fragment:

public class Store_Swipe extends Fragment {

    public static final String ACTION_INTENT = "my_package.action.UI_UPDATE";

    protected BroadcastReceiver receiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            if(ACTION_INTENT.equals(intent.getAction())) {
                String value = intent.getStringExtra("UI_KEY");
                updateUIOnReceiverValue(value);
            }
        }
    };

    private void updateUIOnReceiverValue(String value) {
        // you probably want this:
        kata_keyword.setText(value);
    }

    String KeyWord;
    private TextView kata_keyword;

    public static Fragment newInstance(Context context) {
        Store_Swipe f = new Store_Swipe();
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter(ACTION_INTENT);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(receiver, filter);
    }

    @Override
    public void onDestroy() {
        LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(receiver);
        super.onDestroy();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Bundle bundle = this.getArguments();
        KeyWord = bundle.getString("keyword");
        View view = inflater.inflate(R.layout.store_swipe, container, false);
        init(view);

        return view;

    }

    void init(View view) {
        kata_keyword = (TextView) view.findViewById(R.id.keyword);
        ImageView image = (ImageView) view.findViewById(R.id.image_error);
        kata_keyword.setText(KeyWord);
    }
}

And this code I would have from activity, the parameter is the value from EditText:

protected void sendValueToFragments(String value) {
    // it has to be the same name as in the fragment
    Intent intent = new Intent("my_package.action.UI_UPDATE");
    intent.putExtra("UI_KEY", value);
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
}

You would call this from the click listener that you would set in onCreate:

findViewById(R.id.button_id).setOnClickListener(new OnClickListener() {            
    @Override
    public void onClick(View v) {
        String valueThatYouWantToSend = null; /// just the value
        sendValueToFragments(valueThatYouWantToSend);       
    }
});
gunar
  • 14,660
  • 7
  • 56
  • 87
  • thank anyway, @gunar but, i'm a newbie in android...:D can you show me some code, where i have to put **BroadcatReceiver** in onCreate or OnCreateView? – Arsyah Feb 05 '14 at 10:08
  • how about AndroidManifest file? should i configure it for Broadcastreceiver? – Arsyah Feb 06 '14 at 05:41
  • you don't need to; because you're sending message from within the app to its components. – gunar Feb 06 '14 at 05:42
  • i've edit my code like your method, but my child activity (Store_Swipe ) can't receive string from main acitivty (Swipe_Menu)... what's wrong?..please check my edited code – Arsyah Feb 06 '14 at 06:50
  • Did you send the broadcast message from activity? The intent has the same action? – gunar Feb 06 '14 at 06:57
  • one more, is it need a trigger like (pressing a button) to send string in edittext? – Arsyah Feb 06 '14 at 07:05
  • of course ... you need to call `sendValueToFragments` from your button click or on another action that you choose. – gunar Feb 06 '14 at 07:34
  • i've edit your answer code....do you mean like that? to call **sendValueToFragments** in button click listener – Arsyah Feb 06 '14 at 07:55
  • I still don't see who's calling `sendValueToFragments(String value)` – gunar Feb 06 '14 at 07:57
  • i'm sorry for this misunderstand, what i want to do is, how to call **protected void sendValueToFragments(String value)** from **OnClickListener** – Arsyah Feb 06 '14 at 08:07
  • Great..but how i put the parameter(value) to **onCreateView** in Store_Swipe activtiy...you just put it on **updateUIOnReceiverValue** – Arsyah Feb 06 '14 at 08:27
  • from your post `i wanna pass a string from edittext` ... then `valueThatYouWantToSend` is your edittext `getText().toString()`. Makes sense? Also don't forget to upvote an answer if that is useful – gunar Feb 06 '14 at 08:32
  • hi gunar....can you help me to solve [this problem](http://stackoverflow.com/q/21678418/2003393) – Arsyah Feb 11 '14 at 03:32
0

// I think this solution will solved your issue

 // In Main activity put your code -----------------------------------
 public void onPageSelected(int position)
    {

        System.out.println("nilesh");   

        PageOneFragment f = new PageOneFragment();
    f.getText();

    PageTwoFragment ff = new PageTwoFragment();
    ff.setText();

    }

  //in General Class ------------------------------------------------
   public class General 
  {
     public static String name="";
  }

  // first Fragment ---------------------------------------------
   public void getText()
  {
    General.name = edittext.getText().toString();
  }

  // second Fragment ----------------------------------------------
   public void setText()
   {
      System.out.println("name**" + General.name);
      tv.setText(General.name);
  }
Nilesh Patel
  • 137
  • 6