Javacadabra
Javacadabra

Reputation: 5768

Passing String to Fragment

I have an EditText view in my MainActivity that the user can input a url. This MainActivity also contains a fragment which will hold a WebView.

I have it set up so that when the fragment is displayed the url will load in the WebView. However I don't know how I can pass the String to the Fragment?

Below is the Main Activity code:

public class MainActivity extends FragmentActivity {

Button goBtn;
EditText urlInput;
String url;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    goBtn = (Button)findViewById(R.id.button1);
    urlInput = (EditText)findViewById(R.id.editText1);

    goBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            url = "http://"+urlInput.getText().toString(); //THIS TO FRAGMENT!

            Toast.makeText(v.getContext(), "Search:" + url, Toast.LENGTH_SHORT).show();
        }
    });

and this is the fragment code:

    WebView webDisplay;
String url;
AsyncHttpClient client;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.frag1_layout, container, false);
    urlDisplay = (TextView)v.findViewById(R.id.textView1);
    webDisplay = (WebView)v.findViewById(R.id.webView1);
    return v;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    //url = "http://"+this.getActivity() ???
    client = new AsyncHttpClient();
    client.get(url, new AsyncHttpResponseHandler(){

        @Override
        public void onSuccess(String response) {
            Toast.makeText(getActivity(), "Success!", Toast.LENGTH_SHORT).show();
            webDisplay.setWebViewClient(new WebViewClient() {
                public boolean shouldOverrideUrlLoading(WebView view, String url) {
                          view.loadUrl(url);
                          return true;
                }}); 
            webDisplay.loadUrl(url);
        }
    });

The value I am concerned about it the String URL variable in MainActivity.java.

The transaction of the fragments is controlled by a class TabFragment.

public class TabFragment extends Fragment {

private static final int TAB1_STATE = 0x1;
private static final int TAB2_STATE = 0x2;
private int mTabState;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    View v  = inflater.inflate(R.layout.fragment_tab, container, false);
    //References to buttons in layout file
    Button tabBtn1 = (Button)v.findViewById(R.id.tabBtn1);
    Button tabBtn2 = (Button)v.findViewById(R.id.tabBtn2);

    //add listener to buttons
    tabBtn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            //Action to perform when Tab 1 clicked...
            goToTab1View();

        }
    });

    tabBtn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Action to perform when Tab 2 clicked...
            goToTab2View();
        }
    });

    return v;

}

//Tab action functions.
protected void goToTab1View() {
    if(mTabState != TAB1_STATE){
        mTabState = TAB1_STATE;
        FragmentManager fm = getFragmentManager();
        if(fm!= null){
            //Perform fragment transaction
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.fragment_content, new FragTab1());
            ft.commit();
        }
    }
}

protected void goToTab2View() {
    if(mTabState != TAB2_STATE){
        mTabState = TAB2_STATE;
        FragmentManager fm = getFragmentManager();
        if(fm!= null){
            //Perform fragment transaction
            FragmentTransaction ft = fm.beginTransaction();
            ft.replace(R.id.fragment_content, new FragTab2());
            ft.commit();
        }
    }

}

Upvotes: 1

Views: 3274

Answers (2)

Nikola Despotoski
Nikola Despotoski

Reputation: 50588

Or create Bundle and set it using method fragment.setArguments(myBundle);

Upvotes: 6

Serj Lotutovici
Serj Lotutovici

Reputation: 4380

This answer is in case the fragment already exists

You can define a public method in your fragment. Something like:

public void setString(final String str) { //... }

After that make your activity to implement OnClickListener interfave. And when the button is pressed just type something like that:

public void onClick(View view) {
// Form url...
((YourWebViewFragment) getFragmentManager().findFragmentById(fragments_id)).setString(url);
}

Upvotes: 2

Related Questions