How to send in Android a variable from 1 class to another - java

I have 3 Activities(First,Second,Third) and want to send a variable from First Activity to the Third and use it with a value,which I gave it in First Activity. I found information only how to send variables from 1st to 2nd Activity.

StartActivityForResult
First activity can call third with the variable you want. Once third activity is finished it will send the
Results back to first activity.
Just search for Android startactivityforresult

Related

variable set to null when i change activity

i working on an android application, i have an api object that initialised to null in the main activity, i initilise it in another activity, and when i finish i call back my main activity like this:
startActivity(new Intent(this, MainActivity.class));
when i come back to the main activity, it set up my api object to null;
i don't know how i do to keep my api object set up, when i come back to the main
i tried use static and protected but it's still not work
Would someone have a solution to avoid this problem?
your mistake is that you start MainActivity again, you can just finish second activity.
if you want to send data from second activity to MainActivity you can use onActivityResult method
check this to read more link

Passing values between several activities

I've an issue with passing a variable back to the first activity.
My app starts with one activity, which opens a second one, which opens an third one, which opens a forth, which opens the first activity again.
Now I want to get a variable, which I get from a user input in the third activity in my first one. I already managed to pass variables between two activities there and back with onActivityResult() but I do not get how to manage this between more than two activities.
use bundle
you can use Bundle for move the value from first activity to second activity
check this link ---> [here] (Passing a Bundle on startActivity()?)
if use value in several activity you can use SharePrefrence or you can make
class extends Application and make value in the class and use the values in several activity
be careful if close the app destroy the values
You can use shared preferences to access variables in all your activities or use can use this method:
When going from fourth activity to first use startActivity(intent) and add the variable as an extra in intent. And in first activity override onBackPressed. This may not be good practice but it works.

Can use the intent-filter in android to start a method

me and a partner are working on a project and have designed an app that uses nfc, we want it so that when you're off the app you can tap an nfc card and read it as well, the problem is we don't specifically have an activity for reading or writing a card, its triggered using a button on main activity that starts a dialog fragment, starts a class and reads the card
is it possible, using the intent filter (or any other way without having to create an activity for it) trigger a method in the activity? at the moment we just have it to bring you to the main activity but we want to start a method in main activity when that happens.
Intent filters start Activities. Activities always start with onCreate. If you want to call a single function in your activity, your code is mis-architected and that function should be in a common helper class, not in your activity.

How would I pass data from multiple activities to a single activity in Android?

This may seem like a dumb question, but let's say I have an app where I have a multi-activity form:
Activity 1, 2, 3,..., n
At the bottom of activity 1 to 1-n, there is a "next" button which would ideally send the user-entered data to activity "n" and ALSO moves from activity "k" to activity "k+1" where "k < n".
In activity "n", there's a button which sends all the data from it's form and the previous forms to a database.
Do I need to pass data from activity to activity like a relay race or would I be able to directly pass data from activity "k" to the "n"th activity while also launching the "k+1"th activity? If the latter is possible, how would I do that?
FYI I'm sort of a newbie to Android dev as you can probably tell.
You dont need to pass it to each activity. You can just use a Shared Preference in each activities to save data, and finally in the Last Activity where you can fetch the data from Shared Preference and submit it to the database
Here is the example how it is done in Android
https://www.tutorialspoint.com/android/android_shared_preferences.htm
There are many other ways to achieve though. this would be a simple and basic one.

Starting activity/collecting data/destroying

I would like to have 2 activities, in one activity 2 textboxes and in another acitivity Map.
When I click on Textbox in first one, to open another one, pick place, click Done and close Map, destroy that activity, and fill 1st text box in first acitivty with choosen place. When I click another textbox, need to open same map acitivity but from start.
Any tips how to do that in a right way, opening 2nd acitivty, transfer data in textbox in first one, destroy 2nd, and same again.
This question is without code, it doesn't need to post code from acitivities.
Run the second activities intent with startActivityForResult() method. After user picked the place in the map, but the put the String that you got from your map into intent before finishing second activity. Then at your first activities onActivityResult method, get the String you put into Intent and set it into your Textbox.
You can find detailed tutorial here:
http://developer.android.com/training/basics/intents/result.html

Categories