How to send data from fragment to Activity in Android - 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);

Related

Not getting passing data from Activity to activity

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

kotlin startactivity with flags

I am trying to start an acitvity through Anko context using kotlin, But would like to use flags
override fun createView(ui: AnkoContext<MyActivity>) = with (ui) {
verticalLayout {
// load something
button ("Back") {
onClick {
// goes back to the previous activity
startActivity<PreviousActivity>()
}
}
}
}
I am raising an activity like so,
startActivity<PreviousActivity>()
How can I add the flags to reorder the activity to top
This did not work, get a type mismatch error
startActivity(intentFor<PreviousActivity>("id" to 5).singleTop())
https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Intents
I think singleTop() is what you're looking for
startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
I solved it using so,
getContext().startActivity(intentFor<PreviousActivity>().addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT))
I use the following code to start an activity with FLAGS
val intent = Intent(this#home_paciente,LoginActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(intent)
finish()
The part of
this#home_paciente
is the context of the activity so you put this#activity_name or replace with applicationContext and
LoginActivity::class.java
is the new activity to launch

is there a way in android studio to call a single activity from couple of other activities?

please help .
Im trying to get access to one specific activity from two other activity so I wont write multiple code .
I send from those different activities the same type of "putExtra" but with different values to identify the source of the activity it came from.
I would like if someone could tell me what Im doing wrong.
Sorry and thanks in advance ...
Is this what you're looking for?
Activity1
Intent i = new Intent(Activity1.this, DestinationActivity.class)
i.putExtra("OriginActivity", "Act1")
startActivity(i)
Activity2
Intent i = new Intent(Activity2.this, DestinationActivity.class)
i.putExtra("OriginActivity", "Act2")
startActivity(i)
DestinationActivity
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras != null) {
if(extras.getString("OriginActivity").equals("Act1")){
// you came from Activity 1
}else if(extras.getString("OriginActivity").equals("Act2")){
// you came from Activity 2
}
etc.
You don't need to send extra values to identify the calling Activity, there's a method called getCallingActivity() which can help you.
But it would only return a non null value if you have invoked your Activity through startActivityForResult()
Here is the sample code:
ComponentName callingActivity = getCallingActivity();
if (callingActivity != null)
{
String activityName = callingActivity.getShortClassName();
if (activityName.endsWith("INVOKING_ACTIVITY_NAME"))
{
//do stuff
}
}

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