简体   繁体   中英

How to pass intent extras to activity and frgment inside it from previous activity

I have two activity, Activity B is where user uploads his profile and once he upload then he will redirected to Activity A which contains Two fragment, fragment one and fragment two

When app launches, user is redirected to Activity A firs which has drawer menu and it displays as Register, Once user click Register then Activity B opens. when user submit button , then I pass extras from Activity B to A which then hides the Register button from menu.

In Activity B

                Intent in = new Intent(B.this, A.class);
                in.putExtra("hideRegistermenu", true);
                startActivity(in);

In Activity A

 if(getIntent().getBooleanExtra("hideRegistermenu", false)) {

        menu.findItem(R.id.register).setVisible(false);

    }

It works fine, My problem is Since I also have Register Button in fragment one at Bottom, So when user upload his/her profile and redirected to Activity A, I also would like to change text of Button in fragment A or hide the Button.

But how do I pass same intent extras to fragment which is inside Activity A also and then based on received intent extras perfom my Task.

Thanks in advance.

In Activity A start activity B using:

 public static final int UPDATED_USER = 20;
startActivityForResult(ActivityA.this,ActivityB.class,UPDATED_USER );

on Activity A implements method OnActivityResult Like this:

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK&&requestCode==UPDATED_USER  ){
        //Your code here
        Bundle bundle = data.getExtras();   //Your new data
        String name = bundle.getString("name");

    }
}

Once You start Activity B and user updated profile finish activity like this:

    Intent data = new Intent();
    data.putString("name","name");
    //put All your data
    setResult(Activity.RESULT_OK, data);
    finish();

Step 1: Passing the data from activity to fragment,

Bundle bundle = new Bundle();
bundle.putString("name", "My name");
// set MyFragment Arguments
MyFragment myObj = new MyFragment();
myObj.setArguments(bundle);

Step 2: Receiving the data to the fragment,

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString("name");
        }
    }

Apply this in your code.

I hope it will help you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM