I've seen this thread: Android: how to use data passed from parent activity in a sub activity? and ready about this method, but I'm wondering if this is outdated. Android has made a lot of strides since then and I don't want to do this the wrong way.
Is this still the method that should be used?
This method is perfectly fine and is still used today. There is one more way in which you can pass the data using Bundle.
Bundles can be used as follows
Intent intent = new Intent(ActivityA.this, ActivityB.class);
Bundle b = new Bundle();
b.putString("name", "abcd");
b.putInt("value", 666);
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivity(intent);
And in the other activity use
Bundle b = getIntent().getExtras();
int value = b.getInt("value", 0);
String name = b.getString("name");
Related
I get input from user in first activity and send that to a second activity. From the second activity, using the user input, I fetch data from the db and want to send that data to a third activity. How do I give the intent from the second activity to the third activity?
Presumably you are starting each activity using an Intent in which case you could pass data as an extra.
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("user_input_key", "user_input");
startActivity(i);
In the second activity onCreate
Bundle extras = getIntent().getExtras();
if (extras != null) {
String userInput = extras.getString("user_input_key");
//Once activity starts use this string to get data from DB and put/get extra in the same way
}
You shouldn't be passing the intent between activities. Identify exactly what you want to pass between activities and put that data in the intent as an extra and retrieve that data in the new Activity onCreate.
I am trying to pass a single string array through about 3 classes to finally have the contents of the array[1] printed to a textview. I've been using intent to achieve this with my arraylists and it works fine. For some reason I'm unable to get it working with a measly string array. Here's what I'm doing.
Origin Activity of String Array:
private String [] decisionInput = new String[1];
textData = etShouldI.getText().toString();
if (!textData.matches("")){
decisionInput[0] = (String.valueOf(textData));
test.setText(decisionInput[0]); //TEST WORKS
//CREATE BUNDLE
Bundle bundle = new Bundle();
bundle.putStringArray("decision", decisionInput);
//SEND BUNDLE DATA
Intent intent = new Intent(this,Pro.class);
intent.putExtras(bundle);
startActivity(intent);}
In my next Activity I've got the following, in order to receive the data, and send it off to the next Activity, and so on...
String[] dPasser = new String[1];
#ONCREATE
//BUNDLE RECEIVER
Bundle bundle = getIntent().getExtras();
dPasser = bundle.getStringArray("decision");
thisText.setText(String.valueOf(dPasser)); //TV currently returns null...
#ONCLICK
//SEND DECISION DATA TO NEXT ACTIVITY
Intent intent = new Intent(this, Next.class);
Bundle b = new Bundle();
b.putStringArray("decision", dPasser);
intent.putExtras(b);
startActivity(intent);
What the $%#& am I doing wrong guys?
You put the code below in a file named data, in your code you then use just it by calling data.array
public class data {
public String[] array = new String[1];
}
But going with just passing through a String[] you shouldn't need bundle.
simply
intent.putExtra("stringArray".String[]);
and get it with
this.getIntent().getStringArrayExtra("stringArray")
I must mention i am new to Android and Java. I am trying this for a weeek to solve.
I have Serializable class wich object are populated with http json, and i am using adapters to populate listviews and everything works fine but when i want to pass to another class one object i it force closes, please if somebody can correct my code.
this void is in Serializable class
public void save(){
Intent intent = new Intent();
Bundle extra = intent.getExtras();
intent.putExtra("title", getTitle());
}
when i try this:
Intent intent = new Intent(this, Fragment2.class); i got error The constructor
Intent(FeedItem, Class<Fragment2>) is undefined
and this is fragment class where i want to use passed object
Intent intent = getActivity().getIntent();
Bundle extras = intent.getExtras();
FeedItem feedItem = (FeedItem)getActivity().getIntent().getSerializableExtra("title");
String title = feedItem.getTitle();
Toast.makeText(getActivity(), title, Toast.LENGTH_LONG).show();
error The value of the local variable extras is not used
Many thanks.
Fragments are added like this
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(resourceidwhereyouwantadd(R.id.view), new YourFragment());
ft.commit();
I'm a PHP Web Developer. When I create a view article page I was passing the id in GET something like article.php?id=10
But now in android if I have an activity which views the article, how to pass to it the article ID to select it's data from the database?
What is my best option?
I assume you have 2 Activities. The first one has a list of articles. When you click on an article you start your second activity.
In you onClick() method:
Intent i = new Intent(Activity1.this, Activity2.class);
i.putExtra("id", 10);
startActivity(i);
In you second activity:
int id = getIntent().getIntExtra("id", 0); //0 is a default value
You have to pass the article id in the intent when calling the ArticleActivity. Here is how to do it:
Intent intent = new Intent(this, ArticleActivity.class);
intent.putExtra("articleId", articleId);
startActivity(intent);
In this code, this is the context of your current activity; articleId is your articleId you want to pass. Then, when you want to retrieve it in the ArticleActivity, you do this in onCreate of the activity:
Bundle extras = getIntent().getExtras();
int articleId = extras.getInt("articleId");
Thats it! Hope that helped.
I am calling an Activity using an Intent, and I need to pass variables to that Activity when is initialized. On iOS you can use a custom initialization using the initWithNibName method. How can a similar thing be achieved on Android?
Here is my code that creates an Intent...
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
startActivity(myIntent);
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.putExtra("mystring",strValue)' <<---put String here
startActivity(myIntent);
and in second Activity...
String str = getIntent.getExtras().getString("mystring");<<get string in second class
and check this
How do I pass data between Activities in Android application?
You can put extra data into the Intent...
myIntent.putExtra("exampleString","This is some extra data");
myIntent.putExtra("exampleNumber",1234);
When you call the Intent, it starts the Activity. in one of the main methods of the Activity, like onCreate(), you can access the Intent and get the extras from it, like so...
Intent callingIntent = getIntent();
String exampleString = callingIntent.getStringExtra("exampleString");
int exampleNumber = callingIntent.getIntExtra("exampleNumber");
You can set extras to the intent:
myIntent.putStringExtra("First key", 1);
myIntent.putStringExtra("Second key", "some string");
And then get it in the new activity
Int extraInt = getIntent().getIntExtra("First key");
String extraString = getIntent().getStringExtra("Second key");
See more in the Intent docs
This can be done with intent extras. For example:
int variable = 6;
Intent myIntent = new Intent(webPush.this, webPushActivity.class);
myIntent.PutExtra("stringLabel", variable);
startActivity(myIntent);