I want to send my location(address) from my fragment activity to another activity and show the address there. I tried for last couple days but not failed every time. please guide me.
Use Intent to pass data from one activity to another
Intent myIntent = new Intent(FirstActivity.java,SecondActivity.class);
myIntent.putExtra("data","**** Your data *****");
startActivity(myIntent);
If you want to pass user derfined Object from one activity to another then you have to implement Serilizable or Parcelable
Check the following links for how to use Serilizable
link1 & link2
use getActivity() to send data into other activity
Intent myIntent = new Intent(getActivity(),SecondActivity.class);
myIntent.putExtra("key","**** Your data *****");
getActivity().startActivity(myIntent);
Related
I have an AppCompatActivity(MainActivity) which has a recyclerview and on clicking the card item it is taking me to the 2nd AppCompatActivity. I am passing the values via intent and sending mCtx.startActivityForResult(intent,1) from the recyclerview_adapter.ViewHolder.
Data passed from activity 2 to 3:
Intent intent = new Intent(2ndActivity.this, 3rdActivity.class);
intent.putExtra("user", user);
intent.putExtra("location", location);
startActivity(intent);
Then after clicking on SUBMIT button in the 2nd Activity it takes me to the 3rd Activity. Here I send data by calling
setResult(AppCompatActivity.RESULT_OK,new Intent().putExtra("user", user).putExtra("location", location));
finish();
After I get Back to the 1st Activity(MainActivity) The onActivityResult() Intent Data is null.
How can I get data when I'm passing data through 3 activities?
I appreciate any help.
Thanks in Advance!
Going from the third activity to the first you MUST set the intent extras again, that is because intent extras are not remembered when going from one activity to another, then to another one again.
I'm learning Android and trying to pass user input from my first activity to several activities but not the second activity. actually I'm going to get more data in my second activity and later use that also in my later activities but not in order. How can I do that?
you make the variable as static or you cab send data as extras.
Set data in intent:
Intent intent = new Intent(FirstActivity.this,SecondActiviy.class);
intent.putExtra("key",value);
startActivity(intent);
Fetch data from intent:
String data = getIntent().getStringExtra("key");
usually we send data from one activity to another by using:-
Intent i=new Intent("<action name>");
i.putExtras("name",data);
startActivity(i);
My question is can we send data from one activity to another by using:-
Intent i=this.getIntent();
i.putExtras("name",data);
setResult(Activity.RESULT_OK,i);
finish();
If yes please explain the concept.
Also,these two classes are in the different projects in Eclipse.My another question is, is it possible to send data through intent to another activity situated in another project??
So you're starting from Activity A and going to Activity B using startActivityForResult()
Now we're in Activity B and want to go back to Activity A:
Intent i = new Intent();
if(getIntent().getExtras() != null) i.putExtras(getIntent().getExtras());
setResult(Activity.RESULT_OK, i);
finish();
Doing something like that will allow you to pass back the extras from the calling Intent (if they exist), which would then be reachable from Activity A's onActivityResult() method.
Yes. You can do so, when you call another intent for a result. ie startActivityForResult(Intent,Request_Code);
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);
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