Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

android - pass string from fragment main activity to fragments activity in viewpager

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);
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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);       
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...