I am trying to pass a value from my Activity to my Fragment but the Bundle is always null.
Activity
CallLogsFragment callLogfrag = new CallLogsFragment();
Intent intent = new Intent(this, DeviceUsageActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", callgroup);
callLogfrag.setArguments(bundle);
this.startActivity(intent);
Fragment which is meant to retrieve value from Activity
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
bundle = this.getArguments();
if (bundle != null) {
int myInt = bundle.getInt("key", 0);
}
View view = inflater.inflate(R.layout.fragment_call_logs, container, false);
load(view);
setupList(view);
return view;
}
try this one
// send value from activity to fragment
CallLogsFragment callLogfrag = new CallLogsFragment();
Bundle bundle = new Bundle();
bundle.putInt("key", "4");
callLogfrag.setArguments(bundle);
// call fragment from activity
getSupportFragmentManager().beginTransaction().replace(R.id.containar, callLogfrag).commit();
// get value in fragment
if (getArguments() != null) {
int status = getArguments().getInt("key");
If I am clear with your question!! You are trying to send data from one activity to the fragment in Another activity.
Then you need to first send data in Intent that starts second activity. Then get the data from intent and set your data to fragment as argument and add the fragment to the parent activity.
//here you go, from first activity
Intent intent = new Intent(this, DeviceUsageActivity.class);
Bundle bundle = new Bundle();
bundle.putInt("key", callgroup);
intent.putExtra("yourdata",bundle);
//start your activity - parent activity of your fragment
this.startActivity(intent);
//then get your data in DeviceUsageActivity in onCreate()
Bundle yourdata = null;
if(getIntent().getExtras() != null) {
yourdata = getIntent().getBundleExtra("yourdata");
}
//Then sent data to fragment
CallLogsFragment callLogfrag = new CallLogsFragment();
callLogfrag.setArguments(yourdata);
// add fragment to parent activity
getSupportFragmentManager().beginTransaction().replace(R.id.containar, callLogfrag).commit();
//rest you know - get bundle argument in fragmet
From Activity to store data
Intent i = new Intent(getApplicationContext(), NewActivity.class);
i.putExtra("key","value");
startActivity(i);
To retrieve data back to fragment,use Context as getActivity if needed only in fragments
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("key");
//The key argument here must match that used in the other activity
}
Related
This question already has answers here:
Send data from activity to fragment in Android
(22 answers)
Closed 1 year ago.
I am a little bit lost in this, so I already passed data from my LoginActivity to my DashboardActivity. And it works now (thank god). But now, I would love to pass that data to a fragment.
I have this on my DashboardActivity:
Intent get = getIntent();
String userId = get.getStringExtra(LoginActivity.EXTRA_ID);
Bundle bundle = new Bundle();
bundle.putString("userId", userId);
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
I have this on my Fragment:
Bundle bundle = getArguments();
String userId = bundle.getString("userId");
int uid = Integer.parseInt(userId);
Is it like this?
Getting data in Fragment and Activity are not done in the same way.
To send extra to a fragment, You can do it this way.
In your Activity Class
Bundle bundle = new Bundle();
bundle.putString("userId", "value you want to send");
FragmentClass frag = new FragmentClass();
frag.setArguments(bundle);
Then in Fragement onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("userId");
return inflater.inflate(R.layout.fragment, container, false);
}
The issue with your code is you're getting the argument with a wrong id.
Right way
String userId = getArguments().getString("userId");
From activity to fragment you should use bundle as below
Bundle bundle = new Bundle();
bundle.putString(LoginActivity.EXTRA_ID, "Extra value");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
Then in your Fragment, get the data for example in onCreate() method
Bundle bundle = this.getArguments();
if (bundle != null) {
String extraId = bundle.getString(key, defaultValue);
}
I need to pass a String from an Activity to a fragment but nothing seems to work for me.
I found this answer on stackoverflow
From Activity you send data with intent as:
Bundle bundle = new Bundle(); bundle.putString("edttext", "From
Activity"); // set Fragmentclass Arguments Fragmentclass fragobj = new
Fragmentclass(); fragobj.setArguments(bundle);
and in Fragment onCreateView method:
#Override public View onCreateView(LayoutInflater inflater, ViewGroup
container,
Bundle savedInstanceState) {
String strtext = getArguments().getString("edttext");
return inflater.inflate(R.layout.fragment, container, false); }
But this doesn't work for me. When I click the button nothing happens.
Is it maybe because the fragment is already created? In my app: ToDoFragment> Activity(pass data to:>ToDoFragment
Here is my code but I don't thing it will provide more info:
Activity.java (inside OnClickListener of a button)
String datePassed = mDate.getText().toString();
String toDoPassed = mEditText.getText().toString();
Bundle bundle=new Bundle();
bundle.putString("key1", datePassed);
//set Fragmentclass Arguments
ToDoFragment myToDoFragment=new ToDoFragment();
myToDoFragment.setArguments(bundle);
ToDoFragment.java (inside oncreateview)
Bundle bundle = this.getArguments();
if (bundle != null) {
mToDoInfo = getActivity().getIntent().getStringExtra(key1);
If I understood correct, then you are not doing it wrong, but you are doing this in wrong scenario. This is on assumption that your fragment is already inflated & you are starting new Activity from it & want the data back as result from it. so what you need is something like this,
inside your ToDoFragment where I believe you are starting activity, do this instead:
startActivityForResult(new Intent(getActivity(), MyActivity.class), 1001);
& override onActivityResult method like:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1001 && resultCode == Activity.RESULT_OK) {
String myData = data.getStringExtra("my_key");
}
}
& now inside onClick of your MyActivity,
Intent intent = new Intent();
intent.putExtra("my_key", "my_data");
setResult(RESULT_OK, intent);
finish();
this should invoke onActivityResult of fragment where we wrote code to get data.
Yes this is because the fragment is already created. Create a method in your fragment class and call it from inside the onClick method of your activity.
Let me make it Easy for you!
ToDoFragment myToDoFragment=new ToDoFragment(datePassed); //easy right?
and in your ToDoFragment class make a constructor with String as input!
You can simply follow this pattern to pass the values from activity to fragment
ToDoFragment.java
public class ToDoFragment extends Fragment {
String date;
public static Fragment newInstance(String date) {
ToDoFragment fragment = new ToDoFragment();
fragment.date = date;
return ToDoFragment;
}
}
In your activity's onclick
String toDoPassed = mEditText.getText().toString();
ToDoFragment myToDoFragment = ToDoFragment.newInstance(toDoPassed);
I am new to Android apps. I'm doing JSON parsing for my listview. When I click on my list Image I need to pass that Image to second activity. I'm trying to pass it using intent and bundles. But I have image URL so I don't know how to pass it. I searched everywhere others are passing bitmaps or id.
Class- FirstActivity
Bundle bundle= new Bundle();
bundle.putString("imageUrl",<url for image>);
Intent i= new Intent(FirstActivity.this,SecondActivity.this);
i.putExtras(bundle);
statrActivity(i)
Class- SecondActivity
Bundle bundle = getIntent().getExtras();
String image_url =bundle.getString("imageUrl");
your first activity
Intent intent = new Intent(MainActivity.this,AnotherActivity.class);
intent.putExtras("IMAGE_URL",your_image_url);
startActivity(intent);
in Second activity where you want data
Bundle bundle = getIntent().getExtras();
String image_url =bundle.getString("IMAGE_URL");
pass image_url from one activity to another activity, and then use picasso lib to show image.
In FirstActivity, in the onClickListener, add this
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtras(SecondActivity.KEY_IMAGE_URL, image_url);
startActivity(intent);
And in SecondActivity
public static final String KEY_IMAGE_URL = "image_url";
#Override
protected void onCreate(Bundle savedInstanceState) {
...
if (getIntent().hasExtra(KEY_IMAGE_URL)) {
String imageUrl = getIntent().getStringExtra(KEY_IMAGE_URL);
}
}
I want to pass a bundle on click of button to new activity.
This is the code for bundle in the class adapter which extends recycleview adapter
private void passBundle (Vendor mItemSelected){
mBundle = new Bundle();
mBundle.putString("VENDOR_ID", mItemSelected.getVENDORID());
mBundle.putString("CAT_ID", "" + mItemSelected.getVEN_TYPE());
mBundle.putString("VENDOR_NAME", "" + mItemSelected.getVENDORNAME());
mBundle.putString("CAT_ID", "" + mItemSelected.getVEN_TYPE());
mBundle.putString("CAT_ID", "" + mItemSelected.getVEN_TYPE());
mBundle.putString("VENDOR_AREA", "" + mItemSelected.getVENDORADDRESS());
Intent in = new Intent(context,Chat_Activity.class);
in.putExtra("Bundle", mBundle);
Context.startActivity(in);
}
and this is the code where i retrieve the bundle data in another activity.
Bundle bundle = new Bundle();
chatVenID = bundle.getString("VENDOR_ID", "");
catID = bundle.getString("CAT_ID", "");
vendorName = bundle.getString("VENDOR_NAME", "");
vendorArea = bundle.getString("VENDOR_AREA","");
Bundle bundle = new Bundle();
In this code, you are creating a new object of Bundle, so you wont' get any values. Change it to
getIntent().getBundleExtra("Bundle")
You can invoke
Bundle bundle = getIntent().getBundleExtra("Bundle");
chatVenID = bundle.getString("VENDOR_ID", "");
in onCreate(Bundle savedInstanceState) method;
new Bunlde() will create a new bundle object, can't get pass value from prev activity.
I want to pass data from login page to main activity which contains my menus which works fine. But the problem is that suppose a user clicks on a particular menu and open another activity lets say activity A. when the user press the go back button to switch to main activity the bundle gives a null pointer exception. Here is my login intent
Bundle m=new Bundle();
m.putString("userid",userid);
Intent intent=new Intent(login.this,main.class);
intent.putExtras(m);
startActivity(intent);
///////////////
main
String userid;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle b=getIntent().getExtras();
}
Above code works fine but the problem arise when main activity creates another activity A. when Activity A want to go back to parent activity (main) activity gives a null pointer exception.
Intent inte=new Intent(A.this,main.class);
startActivity(inte);
Why not store the userid in SharedPreferences.
Setter:
SharedPreferences pref = getApplicationContext().getSharedPreferences("UserAccount", 0);
Editor editor = pref.edit();
editor.putString("userid", userid);
editor.commit();
Getter:
pref.getString("userid", "default_value");
Clear:
editor.remove("userid");
editor.commit();
You are trying access bundle when returning to Main Activity, but you dont create bundle in your intent. Check for it as follows,
if(getIntent().getExtras() != null)
{
Bundle b=getIntent().getExtras();
}
If you want the bundle in activity A then send the bundle to Activity A and when you are coming back from Activity A then call the intent with bundle to main activity again.
Use this:
Intent i = new Intent(this, main.class);
i.putExtra("Key1", "ABC");
i.putExtra("Key2", "123");
// Starts main.class
startActivity(i);
Now in main.class
Bundle extras = getIntent().getExtras();
if(extras!=null){
String a = extras.getString("Key1");
String b = extras.getString("Key2");
}
Hope this will work..