communication between fragment and other activity

2020-05-09 21:37发布

public class FragActivity1 extends Fragment implements View.OnClickListener {

    View view;
    ImageButton name_change__btn, status_change_btn, profile_change_btn;
    ImageView person_dp;
    TextView status_field , name_field;
    .
    .
    . 
    .some code here

I've this class containing changeName method which is changing text of a textview . name_field is written as:

name_field=(TextView)view.findViewById(R.id.name_field);

Now I'm calling this method from a popup button where i'm passing some string value at runtime.

I'm getting nullpointer exception.

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

Here is the code of popup class:

public class Name_Status extends AppCompatActivity implements View.OnClickListener {

    EditText name_change;
    RelativeLayout name_status_edit_field;
    String name;
    Button cancel_name_change , ok_name_change;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.name_status);
         DisplayMetrics dm=new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int width=dm.widthPixels;
        int height=dm.heightPixels;

        getWindow().setLayout((int)(width*.8),(int)(height*.3));
        name_change=(EditText)findViewById(R.id.change_name);
        name_status_edit_field=    (RelativeLayout)findViewById(R.id.name_status_edit_field);

        cancel_name_change=(Button)findViewById(R.id.cancel_change_name);
        ok_name_change=(Button)findViewById(R.id.ok_change_name);
        ok_name_change.setOnClickListener(this);
        name_change.setOnClickListener(this);
       name_status_edit_field.clearFocus();
        name_change.setFocusable(true);
        //name_change.update();
    }

    @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");
                   FragActivity1 obj=new FragActivity1();
                   Log.d("obj created", "obj created");
                   obj.changeName(name);
                   Log.d("obj.changename","obj.changename");
                   Intent intent=new Intent();
                   Log.d("intenthsjjsjs","intent hxjjx");
                   intent.setClass(Name_Status.this , MainActivity.class);
                   startActivity(intent);
               }
    }
}

I even searched and found that fragments can't communicate directly with other fragment or activity. But couldn't understand well. And this popup is a new activity other than the one containing my fragment. So how to communicate between fragment and other activity?

Hope I could explain my problem well. Please try to explain in layman language.

3条回答
Lonely孤独者°
3楼-- · 2020-05-09 22:23

Communication between fragments and the activity they are attached to is explained here. From the fragment to the activity you simply use getActivity for a reference to the activity. From the activity to the fragment you can use the findFragmentById(R.id.fragment_id) method of the FragmentManager for a reference to the fragment you want to communicate with.

Communication between activities is realized through Intent s and their Extras as described here. You basically put your Data in a Bundle and insert it in the Ìntent with putExtras().

In your case you should first have the fragment communicate with its activity and then start a new activity with a Bundle from here.

查看更多
可以哭但决不认输i
4楼-- · 2020-05-09 22:24

I suggest you to use EventBus for communicating between fragments and activities https://github.com/greenrobot/EventBus

查看更多
登录 后发表回答