Passing values between several activities - java

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.

Related

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.

Changing something in a view depending on the sequence of activities

Is it possible to change something in a view depending on the sequence of activities? For example, in an adapter I would like to highlight a text, corresponding to an element clicked in a list of elements of a previous activity. But this, only if this adapter is the result of a sequence of certain activities, given that it is used in a fragment, hosted by an activity that can in turn be called up by one of two other activities.
You can keep history of every activity opened by keeping a list/stack of ids (integer or string). First activity that starts in your app will make a new empty list and record itself into it. When another activity is opened you pass that list in the intent. Next Activity extracts the list and adds its own id in it then passes it to next Activity in intent. This way, any activity will have a history of all activities opened previously.
If you want to send additional information along with ids you can make a Parcelable class and pass that along.

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

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

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

Android moving 'windows'

I'm new to android and I'm need a little help understanding how to move from one window to another. I know i use setContentView(R.layout.main) to load an xml layout file, but how do i swap to another layout file? I assume i would use an onClick method on a button and change setContentView(R.layout.other_layout), but would doing all this inside my main activity make my code cluttered? I could end up having 10000+ LOC easily. Can someone explain the correct way to do this please. Thanks
Intents allow us to call another activity from our present activity. For example our current activity is Act1 and we want to move to another activity, Act2. this can be done as:
Intent i = new Intent(Act1.this, Act2.class);
startactivity(i);
Refer http://developer.android.com/guide/topics/intents/intents-filters.html for more information on intents and activity.
Another option is to call setContentView() 2nd time to change the layout.
You use Intents to launch other Activities.
In your current Activity (i.e. window), you can do the following code to launch a new Activity
Intent i = new Intent(this, NewActivityName.class);
startActivity(i);
You should create Activities. Activity is equivalent to window/frame concept on desktop. Each activity should have a goal towards user interaction, ie. taking inputs and showing output. In your case create two activies, and both of them should have their own layour XML and a call to setContentView() within onCreate().
On button click use startActivity() to invoke a new one. Keep in mind that these activities are stacked top of one another.
A visible screen in Android is represented by an Activity. So instead of loading a different layout file into the same activity, you simple create a new activity with it's own layout and java file.
In order to call this second activity from the first one, or to communicate between activities in general android uses so called Intents.
Just look that chapter up in Android's Dev-Gui.

Categories