Import text from fragmentactivity to fragment

user4626905

I want to create a common search for different websites at same time.So I imported text of an EditText from an activity to Fragment activity.Now how to import that text to a fragment?

FragmentActivity:

public class MyActivity6 extends FragmentActivity {
TextView mTextview;
ViewPager viewPager = null;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity6);
    viewPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fragmentManager = getSupportFragmentManager();
    viewPager.setAdapter(new MyAdapter(fragmentManager));
    mTextview = (TextView)findViewById(R.id.textView8);

    mTextview.setText(getIntent().getStringExtra("mytext"));
}



class MyAdapter extends FragmentStatePagerAdapter {

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


    @Override
    public Fragment getItem(int i) {
        Fragment fragment = null;


        if (i == 0) {
            fragment = new FragmentA();
        }
        if (i == 1) {
            fragment = new FragmentB();
        }
        if (i == 2) {
            fragment = new FragmentC();
        }
        if (i == 3) {
            fragment = new FragmentD();
        }
        return fragment;
    }

    @Override
    public int getCount() {

        return 4;
    }

    public CharSequence getPageTitle(int position) {
        String title = new String();
        if (position == 0) {
            return "Flipkart";
        }
        if (position == 1) {
            return "Amazon";
        }
        if (position == 2) {
            return "Snapdeal";
        }
        if (position == 3) {
            return "Ebay";
        }
        return null;
    }


}

Fragment :

`

public class FragmentA extends Fragment {
TextView mText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View mainView = (View)
            inflater.inflate(R.layout.fragment1, container, false);
    WebView webView = (WebView)
            mainView.findViewById(R.id.webView12);
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.getAllowContentAccess();
    webSettings.getDatabaseEnabled();
    webSettings.getAllowFileAccess();
    webSettings.getCacheMode();
    webView.loadUrl("http://m.flipkart.com/search?q=");
    webView.setWebViewClient(new WebViewClient());
    // Set up the ViewPager with the sections adapter.
    webView.setWebViewClient(new MyWebViewClient());




    return mainView;}


}
Larry Schiefer

Rather than use the default constructor for your Fragment, follow this common pattern of using a static newInstance() method to pass arguments. Stash these into the arguments bundle then pull them out in the Fragment during its onCreateView() (or wherever you actually need it.)

public class MyFragment extends Fragment {
    ...
    private static final String ARG_TEXT = "_arg_text";
    ...
    public static MyFragment newInstance(String myText) {
        Bundle args = new Bundle();
        args.putString(ARG_TEXT, myText);
        MyFragment ret = new MyFragment();
        ret.setArguments(args);
        return;
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View mainView = (View)
            inflater.inflate(R.layout.fragment1, container, false);
        Bundle args = getArguments();
        String text = args.getString(ARG_TEXT);

        //  Now use the text however you'd lik
        ...
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call FragmentActivity from Fragment

From Dev

refresh fragment UI from fragmentActivity

From Dev

refresh fragment UI from fragmentActivity

From Dev

Open new Fragment from FragmentActivity

From Dev

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

Fragment being launched from FragmentActivity not showing up

From Dev

Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

From Dev

How to pass parameter to Fragment from FragmentActivity

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

How to call a fragment layout from the FragmentActivity to avoid NullPointerException

From Dev

fragment run before fragmentActivity

From Dev

Android problems with fragment and fragmentActivity

From Dev

Refresh Fragment in FragmentActivity

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

From Dev

Android - Intent Acitivity to a Fragment of a FragmentActivity

From Dev

FragmentActivity not showing the right layout for the fragment

From Dev

Import Text from span

From Dev

Access text of EditTextPreference from Fragment

From Dev

ViewPager NullPointerException while modifying FragmentActivity in Fragment

From Dev

passing variable beetween fragmentActivity and his fragment

From Dev

Fragment Transaction not working when using AppCompatActivity or FragmentActivity

From Dev

Check when Fragment is visible inside a FragmentActivity

From Dev

How to combine a ListFragment with a normal Fragment in a FragmentActivity?

From Dev

Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

From Dev

Can i pass an Instance of a FragmentActivity into a Fragment transaction

Related Related

  1. 1

    Call FragmentActivity from Fragment

  2. 2

    refresh fragment UI from fragmentActivity

  3. 3

    refresh fragment UI from fragmentActivity

  4. 4

    Open new Fragment from FragmentActivity

  5. 5

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  6. 6

    Android - How to access a Fragment from a FragmentActivity

  7. 7

    (Android) how to call method in fragment from fragmentActivity

  8. 8

    Android - Back button from FragmentActivity to another Fragment

  9. 9

    Fragment being launched from FragmentActivity not showing up

  10. 10

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

  11. 11

    How to pass parameter to Fragment from FragmentActivity

  12. 12

    How to call fragment from activity without using fragmentactivity?

  13. 13

    How to call a fragment layout from the FragmentActivity to avoid NullPointerException

  14. 14

    fragment run before fragmentActivity

  15. 15

    Android problems with fragment and fragmentActivity

  16. 16

    Refresh Fragment in FragmentActivity

  17. 17

    Fragment transaction with a FragmentActivity instead of a Fragment

  18. 18

    How to get a Fragment instance from FragmentActivity which contains a ViewPager for fragment transactions?

  19. 19

    Android - Intent Acitivity to a Fragment of a FragmentActivity

  20. 20

    FragmentActivity not showing the right layout for the fragment

  21. 21

    Import Text from span

  22. 22

    Access text of EditTextPreference from Fragment

  23. 23

    ViewPager NullPointerException while modifying FragmentActivity in Fragment

  24. 24

    passing variable beetween fragmentActivity and his fragment

  25. 25

    Fragment Transaction not working when using AppCompatActivity or FragmentActivity

  26. 26

    Check when Fragment is visible inside a FragmentActivity

  27. 27

    How to combine a ListFragment with a normal Fragment in a FragmentActivity?

  28. 28

    Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

  29. 29

    Can i pass an Instance of a FragmentActivity into a Fragment transaction

HotTag

Archive