0

My Activity class(LoginActivity) that have a username i wanna to pass it to Fragment (protifilefragment) BUT it dosen't work idk what the problem.Please Help me

THE ACTIVITY :

 boolean isExist = Mydb.checkUserExist(editusername.getText().toString(), editpassword.getText().toString());
                       if(isExist==true){
                        Intent intent = new Intent (LoginActivity.this,DressyActivity.class);

                        //PASSING DATA TO protfilefragment
                           Bundle bundle=new Bundle();
                           bundle.putString("uesername",editusername.getText().toString());
                           protfileFragment f=new protfileFragment();
                           f.setArguments(bundle);
                           //

                           startActivity(intent); }

THE FRAGMENT:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View view=inflater.inflate(R.layout.fragment_profile ,container,false);


    //RECIVE DATA
     welcome=(TextView)view.findViewById(R.id.textView2);
    if(this.getArguments()!=null){
        String passed_data=this.getArguments().getString("username");
        welcome.setText(passed_data); }
    //

    return view;
}
CoolMind
  • 26,736
  • 15
  • 188
  • 224
DR131
  • 5
  • 5

1 Answers1

0

The problem is you are Starting DressyActivity and passing data to fragment,

Since the profileFragment is handled by it's parent Activity, you should pass data to it's parent activity first. And this will be the dataflow.

LoginActivity → DressyActivity → profileFragment

So in LoginActivity

Intent intent = new Intent(LoginActivity.this,DressyActivity.class);
intent.putString("key","value");
startActivity(intent);

then inside DressyActivity on onCreate() method pass data to profileFragment

// Receive Data From LoginActivity
String value = getIntent().getStringExtra("key");

// Then passs that data to Fragment
Bundle bundle = new Bundle();
bundle.putString("key","value");
profileFragment profilefrag = new profileFragment();
profilefrag.setArguments(bundle);
getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_fragment_container,profilefrag,"frag_profile")
                .commit();

Then you can receive data in profileFragment

if(this.getArguments()!=null){
    String passed_data=this.getArguments().getString("key");
    welcome.setText(passed_data);
}
Suresh Chaudhari
  • 648
  • 1
  • 8
  • 21