PutExtra in android - returning null in Fragment - android

I am having a problem with one of my arguments in putExtra.
I am declaring like this:
Intent upanel = new Intent(getApplicationContext(), MainActivity.class);
upanel.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
upanel.putExtra("USER_ID",json_user.getString(KEY_UID));
upanel.putExtra("USER_FN",json_user.getString(KEY_FIRSTNAME));
upanel.putExtra("USER_LN",json_user.getString(KEY_LASTNAME));
upanel.putExtra("USER_PI", json_user.getString(KEY_PROFILEURL));
pDialog.dismiss();
startActivity(upanel);
and in my fragment I am reciving them like this
userID = this.getArguments().getString("USER_ID");
userFirstName = this.getArguments().getString("USER_FN");
userLastName = this.getArguments().getString("USER_LN");
userProfileURL = this.getArguments().getString("USER_PI");
userid, userfirstname and userlast name work fine, userProfileURL is always returning as null?
the value being sent is /var/www/image/1.jpg, the only thing I can think of is that it doesnt like the / characters? Is this likely and if so how do I escape this character?

Ok well this fixes it!
userprofileurl = getActivity().getIntent().getExtras().getString("USER_PI");
EDIT
Actually, I forgot to declare the put in my MainActivity, in my case the user process is loginactivity->mainactivity and the fragments are loaded from here, so initially I was setting them in my loginactivity and then not in my mainactivity to pass them to the fragment
a d'oh moment!

Related

How to send data from Activity to Fragment?(Android)

I want to make use of some TextViews(cityField, updatedField) I have in my activity inside my fragment.
I know it would have been easier to make use of them in the activity instead, but the problem is that I have to join in with some codes on Fragment due to getting some JSON data
Already I've gotten the id for the codes on activity
cityField = findViewById(R.id.textView4);
updatedField = findViewById(R.id.textView9);
Now I want to make use of them in my fragment
So the question is - is it possible? if it's possible, how?
Already, I checked some answers on this site - Send data from activity to fragment in Android
How to pass data from Activity to Fragment using bundle
but they directly didn't solve my problem.
You should create a SharedViewModel and let the Activity set values of variables like cityField and updateField while the Fragment Observe the changes done to them, basically the Activity will Set and Fragment will Get
for more information about ViewModels check this:
https://developer.android.com/topic/libraries/architecture/viewmodel
You can access these instances from your fragment like this:
String cityFieldFragment = (activity as YourActivity).cityField;
String updatedFieldFragment = (activity as YourActivity).updatedField;
If I understood this right, the fragment lives in your activity, so you are able to access the data from it, just make sure that the instances are public.
if the fragment is not already created you can make use of the constructor of the fragment and pass arguments to it.
If the fragment is already created, create a method inside the fragment and invoke it in the activity.
Let's say you want to pass it from an activity to another activity that contains fragments. First, you need to pass the data like so :
Intent intent = new Intent();
intent.setClass(FirstActivity.this, SecondActivity.class);
intent.putExtra("name", username);
intent.putExtra("pass", password);
startActivity(intent);
In this case, the fragment is inside the SecondActivity.java but to receive the data, we need code inside the Fragment.java instead of the SecondActivity.java.
Bundle extras = getActivity().getIntent().getExtras();
if (extras != null) {
getUsername = extras.getString("name");
getPassword = extras.getString("pass");
}
I haven't tried to pass the data directly to the fragment, but you can try with the same code. All you need to do is changing the intent instead of intent.setClass(FirstActivity.this, SecondActivity.class); to intent.setClass(FirstActivity.this, Fragment.class);.
Have you tried with SharedPreferences?
in you MainActivity
// create sharedPrefences file with the name "info"
SharedPreferences sp = this.getSharedPreferences("info",0);
// here the name and default value
sp.getString("name", "default value");
// create editor
SharedPreferences.Editor editor = sp.edit();
// enter a new value
editor.putString("name", "new value");
// save changes
editor.apply();
in your Fragment
SharedPreferences sp = getActivity().getSharedPreferences("info",0);
String str = sp.getString("name", "default value"); //will get you the new value you entered

How to send data from fragment to Activity in Android

In my application i want use Fragment and Activity and i want startActivity from this fragment and i should send data with startActivity
I write below codes, but when running application show null in activity!
Fragment codes :
homeDashboard_giftInfoLayout.setOnClickListener {
val intent = Intent(requireContext(), DashboardChargeWalletActivity::class.java)
intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
intent.putExtra(USER_CHARGED_VALUE, 0)
startActivity(intent)
}
Activity codes :
userWallet = intent?.getStringExtra(USER_WALLET_PRICE).toString()
userWallet.let {
//User wallet
dashboardChargeWallet_userWallet.text =
"${getString(R.string.walletInventory)}: $userWallet ${getString(R.string.toman)}"
}
when click on button for startActivity i show this response.user.wallet.credit into toast.
But into activity show me null !
I used Kotlin for language.
How can i fix it?
this line:
intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
I suspect that response.user.wallet.credit isn't a string. So the below line:
userWallet = intent?.getStringExtra(USER_WALLET_PRICE).toString()
won't find a string extra with USER_WALLET_PRICE key. Check what type response.user.wallet.credit is, int? long? serializable? and make sure you get it by the same type
you need to use intent like this from a fragment.
homeDashboard_giftInfoLayout.setOnClickListener {
val intent = Intent(activity, DashboardChargeWalletActivity::class.java)
intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
intent.putExtra(USER_CHARGED_VALUE, 0)
startActivity(intent);
}
also you can use requireActivity
val intent=Intent(requireActivity(),DashboardChargeWalletActivity::class.java)
Hopefully this will work for you.
try this, it may help
Intent intent = new Intent(getActivity(), LoadActivity.class);
intent.putExtra(USER_WALLET_PRICE, response.user.wallet.credit)
intent.putExtra(USER_CHARGED_VALUE, 0)
startActivity(intent);

Passing String from one class to another in Android

I want to transfer a String value from one class to another class in same package. The second class is called using intent in onclick of a menu item. I used the code
Intent i = new Intent(TouristGuideActivity.this, PointOfInterest.class);
i.putExtra("videoId", videoId);
startActivity(i);
in first class and then in second class,
String address=getIntent().getExtras().getString("videoId");`
But when I click on the menu item, I get a force close. If I remove that put Extra part, it works ine. But in that case I can't send the string. Please help!
Intent intent=new Intent(this,secondclass.class);
intent.putExtra("videoId",videoId);
startActivity(intent);
and in your second class try 2 get these items by doing
Bundle extras = getIntent().getExtras();
if(extras!=null){
String videoId=extras.getString("videoId");
}
just after u define your class i hope dis would help u out 2 get the values on other page
Make sure that you are getting the same type, for example, if you put a String, get a String.
Then try with:
getIntent().getStringExtra("videoId");

Using integer from one class in another Android

I need some help with using integer from one activity to another.
I am making some basic math program(game). It gets two random numbers, random operator, and 30 secs to solve math problems as much as you can.
If you solve problem u get 1 point.
Anyway right now, I want to get number of points that user have made and use it in another activity called *RankActivity*.
Main activity is called *BrzoRacunanjeActivity* and it contains button and one *int* called *poenibrojanje* which get number of points that user have made, and when I click on button, it opens new Activity with this line:
startActivity(new Intent(this, RankActivity.class));
As you can see another Activity is called RankActivity, and there I wrote :
*BrzoRacunanjeActivity a1 = new BrzoRacunanjeActivity();*
*System.out.println("Number of points:" + a1.poenibrojanje);;*
and I get all time this reuslt: 09-22 09:09:14.940: INFO/System.out(289): Number of points:0
Try this:
Intent intent = new Intent(this, RankActivity.class);
intent.putExtra("points", pointsVar);
startActivity(intent);
In onCreate of RankActivity:
getIntent().getIntExtra("points", 0);
so you want to pass integer value from one activity to another activity? right...
try:
Intent intent = new Intent(currentclass.this, destination.class);
intent.putExtra("point", pointvalue);
startActivity(intent);
at destination activity:
final int getpoint = getIntent().getExtras().getInt("point");
This will solve your problem.
first of all make
static variable like as public static int poenibrojanje;
in your BrzoRacunanjeActivity.class file now you can use this variable in any other class like as
BrzoRacunanjeActivity.poenibrojanje
or you can use putExtras(); method.
in you main activity.
Intent i = new Intent(this, RankActivity.class);
i.putExtra("Value",poenibrojanje);
in your next activity
int v = (getIntent().getExtras().getInt("Value")) ;

My Activity not reading data

Android 2.1 update 1
Eclipse 3.5
I have a problem reading data from my 2nd activity that is called from the 1st activity using intent. I have androidmanifest.xml setup correctly.
My First Activity has the following code:
Intent myIntent = new Intent(MainMenu.this, Testmenu.class);
myIntent.putExtra("com.tweaktool.MyAge",40);
myIntent.putExtra("com.tweaktool.Enabled", false);
startActivity(myIntent);
My 2nd Activity has the following code:
Bundle bun = getIntent().getExtras();
int myAge = bun.getInt("MyAge");
boolean enabled = bun.getBoolean("Enabled");
When I look at the above code in 2nd Activity it lists the following:
enabled = false
myAge = 0
Why is this doing this??? Am I doing something simple wrong??
You're putting data with one keys ("com.tweaktool.MyAge", "com.tweaktool.Enabled") and trying to get it with others ("MyAge", "Enabled") -- the bundle then just returns you defaults (0, false). To get what you've put, use the keys you've used.
Have your tried
int myAge = bun.getInt("com.tweaktool.MyAge");?

Categories

Resources