I'm developing an android application where I fetch all user data from firebase in an Activity class(the Dashboard Activity) and then using Bundle all user data is stored and is transferred to one of the fragments of Dashboard Activity depending on if user was an admin or employee.
Now the situation here is.. In that fragment I am not unpacking the bundle(i.e. I am not creating separate string variable to save every value it received from its parent, the Dashboard Activity) Instead from that fragment I want to further send this data(the bundle object) as it is to another activity(the AccountInfo Activity), thats where I am supposed to extract the values from bundle). Can someone tell me how to send that bundle object to AccountInfo Activity, and then how can i get the values from that bundle object in other activty. There are couple of similar questions asked prev but none of them exactly points my issue. Can someone help? Maybe I'm missing a slight something only.
I am doing this to send the bundle:
//FRAGMENT under DASHBOARD ACTIVTY
public class DshAdminFragment extends Fragment implements View.OnClickListener {
private final static String TAG= "admin-fragment";
CardView cvProfile, cvMngEmployee, cvReport;
Bundle bundle, data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Here it gets the bundle object from its parent the DASHBOARD ACTIVTY
bundle = this.getArguments();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_dsh_admin, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
cvProfile = view.findViewById(R.id.crd_profile);
cvMngEmployee = view.findViewById(R.id.crd_mng_employee);
cvReport = view.findViewById(R.id.crd_emp_att_report);
cvProfile.setOnClickListener(this);
cvMngEmployee.setOnClickListener(this);
cvReport.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.crd_profile) {
Intent intent = new Intent(getContext(), AccountInfoActivity.class);
intent.putExtra("bundle", bundle);
startActivity(intent);
}
}
}
Now, thats how I am receiving it but idk how to get the values from it like the name, email etc sent from Dashoabord Activity as bundle to its Fragment and from there to this AccountInfo Activity.
//ACCOUNT INFO Activity
public class AccountInfoActivity extends AppCompatActivity {
private static final String TAG = "acc-info-activity";
Bundle bundle;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_info);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
try {
Bundle bundle = getIntent().getExtras();
if(bundle != null){
//name= bundle.getString("bundle");
}
Log.e(TAG, String.valueOf(bundle));
} catch (Exception e) {
Log.e(TAG, "An error occured.." + " " + e);
}
}
}
here I see that you have received the bundle of your Dashboard activity into a fragment bundle = this.getArguments();
.
And from this you have shared this bundle to your another activity here :- intent.putExtra("bundle", bundle);
.
Instead of this you can simply use (if you are not using your bundle anywhere else in the fragment or else bundle()
will also work instead of this.getArguments()
)
if (this.getArguments != null) {
intent.putExtras(this.getArguments())
}
Please note use intent.putExtras
instead of intent.putExtra
.
so now in your Dashboard Activity you will get bundle using this :-
Bundle extrasBundle = getIntent().getExtras()
Now you can extract all the data from this extrasBundle
using extrasBundle.getString("YOUR_KEY");