How to pass the article id to the "View Article" Activity - java

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.

Related

How to set an intent so that I can send a data to the second activity and from there to the third activity?

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.

Show text on other activity than the button clicked

I know how to show text with a button click on the same page, but my question is (since I couldn't find anything on Google): Is it possible when you click a button, that the text shows up on another activity?
Yes, you can
In your FirstActivity execute this when button is clicked:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data","Messsage to be sent");
startActivity(intent);
In your SecondActivity inside onCreate():
String someData = getIntent().getStringExtra("data");
yourTextView.setText(someData);
If you need pass values between the activities you use this:
String name="aaaa";
Intent intent=new Intent(Main_Activity.this,Other_Activity.class);
intent.putExtra("name", name);
startActivity(intent);
And this code to recovery data on new Activity:
Bundle b = new Bundle();
b = getIntent().getExtras();
String name = b.getString("name");
You can use Intent for this. Intent is used to move on other activity from first activity.
You can use :
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("YourValueKey", yourData.getText().toString());
then you can get it from your second activity by :
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("YourValueKey");

How to synchronize data between Activities?

I want data to be synchronized between two Activities. I have TextView1 inside 1st Activity and TextView2 inside 2nd Activity. The 2nd Activity is started form the 1st. After that the data in TextView2 is changed. When I'll be back to the 1st Activity, the data in TextView1 has to be the same with TextView2's data. I've tried to use intents, but it's not possible to be as the 1st Activity is crashed because it's waiting for the data, I suppose.
The 1st Activity:
.....
level = getIntent().getExtras().getString("level");
score = getIntent().getExtras().getString("score");
.....
The 2nd Activity:
.....
Intent intent = new Intent(2nd_activity.this, 1st_activity.class);
intent.putExtra("level", Integer.toString(level));
intent.putExtra("score", Integer.toString(score));
.....
I guess you have figured it out why it doesn't work.
What do I need to do to solve this problem?
You can use startActivityForResults to open 2nd Activity, when 2nd activity is supposed to be closed then you call:
Intent returnIntent = new Intent();
returnIntent.putExtra("tv_text",tv.getText());
setResult(RESULT_OK,returnIntent);
and in activity 1, you will receive results in onActivityResult and update textview in activity 1 with Intent data, sample code is from:
How to manage `startActivityForResult` on Android?
You have to check that the call to getIntent() does not return null, as is the case when you first launch 1st Activity
Intent rcvdIntent = getIntent();
if (rcvdIntent != null) {
level = rcvdIntent.getExtras().getString("level");
score = rcvdIntent.getExtras().getString("score");
}

Pass data from sub activity to parent activity

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");

Passing extra data from an Activity to an Intent

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);

Categories

Resources