Abhishek Kumar
Abhishek Kumar

Reputation: 4808

How to send text from activity to fragment of other activity

I've two activity "Name_Status" and "Main_activity". In Main_activity , i've fragments . i wanna send a string value from Name_Status activity to one of Main_activity fragment.

Below is the method of Name_Status activity:

@Override
public void onClick(View v) {

    if (v.getId() == R.id.ok_change_name)

    {
        name = name_change.getText().toString();
        System.out.println(name);
        Log.d("entered", "entred");
        MainActivity obj=new MainActivity();
        Log.d("obj created", "obj created");
        obj.changeMainName(name);
        Log.d("obj.changename", "obj.changename");

    }
}

By using this method i'm first sending data to the Main_activity and from there sending data to fragment as below.

Below is the code of Main_activity:

public void changeMainName(String s) {
    FragActivity1 obj = (FragActivity1) getSupportFragmentManager().findFragmentById(R.id.person_profile); obj.changeName(s); 
}

Now from here I'm sending string value to fragment class and there I'm only setting the string to the textview like below:

public void changeName(String s)
{

    Log.d("changename entry" ,"changename netry");
    System.out.println(s);
    System.out.println(name_field.getText().toString());
       name_field.setText(s);
    System.out.println(name_field.getText());
}

I'm getting

NullPointerException at obj.changeMainName(name);

Upvotes: 0

Views: 958

Answers (2)

CodeDaily
CodeDaily

Reputation: 766

You gonna have to do some relay. For instance you have activity A which has no Fragment and Activity B which has a fragment. On Android framework you can first pass data to From Activity A to B and From Activity B you can pass data to its Fragment following the view Hierarchy.

In Activity A. do the following

Intent intent = new Intent(this, ActivityB.class);
        intent.putExtra("key", "value");
        startActivity(intent);

Now inside Activity B, get data from activity A

String stringValue= getIntent().getStringExtra("key");

Then pass that data your Fragment as an argument before to start Frag

Bundle bundle = new Bundle();
    bundle.putString("key", stringValue);
    ActivityBFrag fragment =  new ActivityBFrag();
    fragment.setArguments(bundle);

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(container, fragment, tag);
    transaction.addToBackStack(tag);
    transaction.commit();

Then inside your Fragment class. get data like below whenever you wanna access your data, can be inside onCreate, OnActivityCreated etc.

public void getArguments(){
    Bundle bundle = getArguments();

    if(bundle != null){
        String stringValue = bundle.get("key");
    }
}

Upvotes: 3

Md Sufi Khan
Md Sufi Khan

Reputation: 1761

Send data to second activity by Intent bundle. Then get the data in second activity by getIntent().getExtras() and then pass the data to fragment.

Upvotes: 2

Related Questions