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

Get id of TextView in Fragment from FragmentActivity in ViewPager

From Dev

Fragment transaction with a FragmentActivity instead of a Fragment

From Dev

ViewPager NullPointerException while modifying FragmentActivity in Fragment

From Dev

Call FragmentActivity from Fragment

From Dev

fragment run before fragmentActivity

From Dev

Android - Intent Acitivity to a Fragment of a FragmentActivity

From Dev

refresh fragment UI from fragmentActivity

From Dev

How to call fragment from activity without using fragmentactivity?

From Dev

Fragment Transaction not working when using AppCompatActivity or FragmentActivity

From Dev

Android - How to access a Fragment from a FragmentActivity

From Dev

passing variable beetween fragmentActivity and his fragment

From Dev

(Android) how to call method in fragment from fragmentActivity

From Dev

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

From Dev

Android - Back button from FragmentActivity to another Fragment

From Dev

Check when Fragment is visible inside a FragmentActivity

From Dev

FragmentActivity not showing the right layout for the fragment

From Dev

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

From Dev

Android problems with fragment and fragmentActivity

From Dev

Fragment being launched from FragmentActivity not showing up

From Dev

Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

From Dev

refresh fragment UI from fragmentActivity

From Dev

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

From Dev

Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

From Dev

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

From Dev

How to pass parameter to Fragment from FragmentActivity

From Dev

Open new Fragment from FragmentActivity

From Dev

Import Text from span

From Dev

Refresh Fragment in FragmentActivity

From Dev

Access text of EditTextPreference from Fragment

Related Related

  1. 1

    Get id of TextView in Fragment from FragmentActivity in ViewPager

  2. 2

    Fragment transaction with a FragmentActivity instead of a Fragment

  3. 3

    ViewPager NullPointerException while modifying FragmentActivity in Fragment

  4. 4

    Call FragmentActivity from Fragment

  5. 5

    fragment run before fragmentActivity

  6. 6

    Android - Intent Acitivity to a Fragment of a FragmentActivity

  7. 7

    refresh fragment UI from fragmentActivity

  8. 8

    How to call fragment from activity without using fragmentactivity?

  9. 9

    Fragment Transaction not working when using AppCompatActivity or FragmentActivity

  10. 10

    Android - How to access a Fragment from a FragmentActivity

  11. 11

    passing variable beetween fragmentActivity and his fragment

  12. 12

    (Android) how to call method in fragment from fragmentActivity

  13. 13

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

  14. 14

    Android - Back button from FragmentActivity to another Fragment

  15. 15

    Check when Fragment is visible inside a FragmentActivity

  16. 16

    FragmentActivity not showing the right layout for the fragment

  17. 17

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

  18. 18

    Android problems with fragment and fragmentActivity

  19. 19

    Fragment being launched from FragmentActivity not showing up

  20. 20

    Calling Fragment Method by Parent FragmentActivity, getting NullPointerException

  21. 21

    refresh fragment UI from fragmentActivity

  22. 22

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

  23. 23

    Sending a string from FragmentActivity to Fragment through FragmentPagerAdaptor

  24. 24

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

  25. 25

    How to pass parameter to Fragment from FragmentActivity

  26. 26

    Open new Fragment from FragmentActivity

  27. 27

    Import Text from span

  28. 28

    Refresh Fragment in FragmentActivity

  29. 29

    Access text of EditTextPreference from Fragment

HotTag

Archive