Not getting passing data from Activity to activity - android

I am not getting data which i passed from another Activity using intent.putExtra. Overall it is also not showing errors.
I am new to Android and Kotlin
Activity One
i2.setOnClickListener(View.OnClickListener {
var i = Intent(this,Courses::class.java)
i.putExtra("semester",'2')
startActivity(i)
})
Activity Two
var semester:String? = null
semester = intent.getStringExtra("semester")
Not getting any data Just null and also not getting error. I tested it to show using Toast

Use Double quotes instead
i.putExtra("semester","2")

Try like this, it should work.
i2.setOnClickListener(View.OnClickListener {
Intent i = new Intent(this,Courses.class)
i.putExtra("semester",'2')
startActivity(i)
})
In second activity try this in OnCreate method:
Intent intent = getIntent();
savedInstanceState = intent.getExtras();
char exampleVariable = savedInstanceState.get("semester");

Related

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);

Intent Extras.getString() not comparing correctly -- Android

I have an Activity called searchProcedures which allows a user to select from a listview of medical procedures. I navigate to this activity from two other activities called searchHome and describeVisit. I needed a way for searchProcedures to know which activity it should navigate back to onClick. So I pass an intent.extra from searchHome or describeVisit (key:"sentFrom" value""). Then in searchProcedures I use the following code to determine which class to navigate to.
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(!extras.isEmpty()){
if(extras.containsKey("sentFrom")){
if(extras.getString("sentFrom") == "searchHome"){
returnIntent = new Intent(searchProcedures.this, searchHome.class);
}
else if(extras.getString("sentFrom") == "describeVisit"){
returnIntent = new Intent(searchProcedures.this, describeVisit.class);
}
else{
Log.d("failed", "the value of getString is " + extras.getString("sentFrom"));
}
}
}
Checking the Log values, the correct values are being passed to and from activity, but when I check extras.getString("sentFrom") == "searchHome/describeVisit" it comes back as false, and returnIntent remains un-initialized. I have tried putting .toString after the .getString to no avail.
1.
== compares the object references, not the content
You should use:
"searchHome".equals(extras.getString("sentFrom"))
Remeber to check blank space,...
2.
You can use a static variable in your SearchProceduresActivity to check where it comes from
SearchProceduresActivity
public static int sFrom = SEARCHHOME;
SearchHomeActivity:
Intent myIntent = new Intent(SearchHomeActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = SEARCHHOME;
startActivity(myIntent);
DescribeVisitActivity:
Intent myIntent = new Intent(DescribeVisitActivity.this, SearchProceduresActivity.class);
SearchProceduresActivity.sFrom = DESCRIBEVISIT;
startActivity(myIntent);
SEARCHHOME, DESCRIBEVISIT value is up to you
Hope this help!
String compare should use equal not ===

Passing data from two activities to a third activity in android

have total 3 activites. I pass the data from the first activity like this:
Intent intent = new Intent(getActivity(), Movie_rev_fulldis_activity.class);
intent.putExtra("mov_pos", position + "");
startActivity(intent);
this working fine all data visible to my second activity but i want to display
one filed item to third activity when i click second activity image
here my second activity
youtube_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent youtube= new Intent(getApplicationContext(),PlayYouTube1st.class);
youtube.putExtra("youtubeLink",youtubeLink);
startActivity(youtube);
// Toast.makeText(Movie_rev_fulldis_activity.this,
// youtubeLink, Toast.LENGTH_LONG).show();
}
});
youtubeLink=Reviews_update.revData.get(mov_pos).getYoutubeLink();
Toast.makeText(Movie_rev_fulldis_activity.this,
Reviews_update.revData.get(mov_pos).getYoutubeLink(), Toast.LENGTH_LONG).show();
mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));
in second activity i am getting values but when i send one filed to third activity that value passing null any one please help me i stuck in their,
I want to show YoutubeLink field sencnd activity to third activity how to parse that any one please help
In FastActivity:
Intent in = new Intent(FastActivity.this, SecondActivity.class);
in.putExtra("a", yourdata);
startActivity(in);
In SecondActivity:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String a = extras.getString("a");
}
You can also use any static variable and use it in any class
static int a = 5;
And in any other class
int b = classname.a;
classname is the class where static variable is declared.
Make that variable static and then pass
Your variable object reference get change in the THIRD Activity so, it returns null

PutExtra in android - returning null in Fragment

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!

Start intent activity back to same activity

I am creating an application, surprising I know, and using a quick back button to return the user to a previous listing. The code below is the intent portion that starts the activity. Now it is sending the activity back to itself with an "LvPos" variable to determine which position it just re-start itself at.
Spinner spinMe = (Spinner)findViewById(R.id.spinner1);
Intent backIntent = new Intent(null, null, getBaseContext(), MainActivity.class);
int itemSelected = spinMe.getSelectedItemPosition();
backIntent.putExtra("LvPos", itemSelected);
startActivity(backIntent);
Now the code below is the reference in the onCreate method that gets teh LvPos variable. The problem is, when I get to this portion, the LvPos is null. I have the same code for various other intents and all work fine. If anyone can see any glaring issues, let me know as I have to be severely overlooking something.
int positionID = 0;
Bundle extras = getIntent().getExtras();
if (extras != null){
String LvPosBundle = extras.getString("LvPos");
if (LvPosBundle != null)
positionID = Integer.parseInt(LvPosBundle);
}
Thank you in advance.
You are Using the putExtra(String, int) when you are putting the extra.
When retrieving you use:
extras.getString("LvPos");
instead use:
extras.getInt("LvPos");
Store that in an integer instead of string. Then you also don't have to do the parseInt.
Hope this Helps.
-Travis

Categories

Resources