Make activities communicate [duplicate] - android

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do I pass data between activities in Android?
I have three activities in my application and each activity is depending on another. I'm currently using static variables to pass objects and values between these activities. Problem with this is that it get's very confusing fast and it's hard to keep track of when I assigned that global variable a value etc. I'm thinking of implementing an interface between these activities to make the code clearer and easier to understand. Thing is, i'm not entirely sure this is the best way to go so any help or advice would be great.

use putExtra to send info to another activity
send:
Bundle bundle = new Bundle();
bundle.putString(“name″, “username”);
Intent newIntent = new Intent(this.getApplicationContext(), ActivityClass2.class);
newIntent.putExtras(bundle);
startActivityForResult(newIntent, 0);
Receive:
Bundle bundle = this.getIntent().getExtras();
String data = bundle.getString(“name″);
data = username

I believe what you want is the Intent.putExtra() method. There are several methods depending on what kind of data you want to pass. See the documenation here.

to pass data:-
Intent i = new Intent();
i.putExtra("key", "data");
startActivity(i);

Related

Is it Possible to Pass Edittext Value from one activity to another activity without using Intent

Intent intent = new Intent(this, Page.class);
intent.putExtra("arg", getText);
startActivity(intent);
//second activity:
String passedArg = getIntent().getExtras().getString("arg");
enteredValue.setText(passedArg);
To get value from one activity to another activity without using
intents....?? I want to show edittext passed data in other activity
edittext but without using intent...?? Kindly help me is it possible
or not....??
There are many ways: event buses, SharedPreferences, through global object (application class), database, simple file. But why?
Try direct and readable style first.
if you can't bundle the values you want to save in the Intent, then you could use the SharedPreferences. More information here

Which function does intent have? [duplicate]

This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance
I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message
Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);
The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity

Passing your own objects between activities

I am a newbie on Android development and how data pass from one activity to another is still unclear for me.
Is it a good practice to pass your own objects between activities?
Which size could they have?
EDIT:
Maybe I did not explained it clearly. I know how to pass data between activities. The question is: Is it a good practice or should I better obtain data from SQLlite database? In that case, which is the maximun size I can pass to have a good performance?
Sending object is same as sending pre defined objects like String or variables,it can be done by binding your object to Intent by using putParceble() or putSerializable() methods directly on intent or by binding object to a bundle object.
But u have to make sure that your class implement either Parcelable or Serializable.
like here:
UserDefined myObject=new UserDefined();
Intent i = new Intent(this, Activity2.class);
Bundle b = new Bundle();
b.putParcelable("myObject", myObject);
i.putExtras(b);
startActivity(i);
And in receiving activity:
Bundle b = this.getIntent().getExtras();
myObject = b.getParcelable("myObject");
you can also send object without using Bundle:
Intent i=new Intent(PicActivity.this,PostPhotoActivity.class);
i.putExtra("myObject", myObject);
startActivity(i);
on receivingActivity:
UserDefined myObj=(UserDefined)getIntent().getParcelableExtra("myObject");
In Android parcelable is prefered instead of serializable.

Constructors in Android Programming?

I have some values and want to pass with activity so that I can show in TextViews of Activity, I am unable to understand such a concept, so what should I do?
Simply I want to make constructor but unable to understand it that how will it be done, I am new to android programming, so needed help.
Start by reading the documentation at developer.android.com about intents and intent extras.
In android if you launched an activity there is a method called onCreate execute automatically.You can send values using Intent to the activity and can retrieve them in the Activity
The Activity (sub)class must have a default constructor without any parameters so that the system can instanciate it at run time.
To pass "parameters" to activities, you need to use the extra bundle of the intent.
Intent i = new Intent(this, MyActivity.class);
i.putExtra("com.sample.MyParameter", 666);
startActivity(i);
See Starting An Activity
If you want to pass one value then you can use intent but if you want to pass multiple values then using "Bundle" is best way.
Bundle bundel = new Bundle();
bundel.putStringArray("key1",strings);
bundel.putStringArray("key2",stringsofids);
bundel.putString("key3", str31);
bundel.putStringArray("key4",stringsbakup);
bundel.putString("key5", str1);
bundel.putString("key6", str4);

working with android intents how to pass arguments between father and the intent h in

Assuming i want to open another activity from my current activity and i want to pass arguments such as in my case difficulty level how do I do it?
newGameButton.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent i = new Intent(countryCityGameMenu.this,GameScreen.class);
startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);
}
});
is there a way to pass those arguments in the calling ?
can someone show an example explaining
what send activity should do
what the new born activity should do
As ben already mentioned you can add this data to the intent inside an extra bundle.
An extra bundle stores data as a key value pair of native java data types.
You can add data to the intent via the putExtra methods.
In the new Activity you can retrieve this data via the getExtra methods of the Intent. For example the getStringExtra method.
To get the intent that started the current activity just use the getIntent() method from activity.
You have to use extras like this:
i.putExtra(varName, value);
Not a big fan of this approach... but unfortunately it sends to be the only way to do it in android...
fine.. U can also use StartActivity(intent); Thats enough to pass

Categories

Resources