Android put extra doesn't send extras to another activity - java

I try to send some values to another activity.
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
Intent intent = new Intent(GroupsMain.this, AboutGroup.class);
intent.putExtra("groupName", "Hello");
startActivity(intent);
}
#Override
public void onLongClick(View view, int position) {
}
}));
And so on AboutGroup activity I try to get extra.
1 way:
Bundle extras = getIntent().getExtras();
String name = extras.getString("groupName");
and second way:
Intent intent = new Intent();
String name = intent.getStringExtra("groupName");
But nothing works for me. On AboutGroup activity i get empty string. Please help me fix this problem.

try this
Send:
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("groupName", "Hello Anna");
startActivity(intent);
get extra:
String name = getIntent().getStringExtra("groupName");
myTextview.setText(name);

Related

Problem receiving data from an Intent in Android Studio

I'm writing code that sends and receives data through Intent
However, the code is always having problems in:
summaryResult
Please, can anyone help me?
Code to send data:
``` nextPage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
Bundle sendlerBundler = new Bundle();
sendlerBlundler.putString("summaryResult", summaryResult);
senderIntent.putExtras(sendlerBundler);
startActivity(senderIntent);
Toast.makeText(getApplicationContext(), "It was sent: " + summaryResult, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), UserRegistration2.class);
startActivity(intent);
Bundle param = new Bundle();
param.putString("name", name);
param.putString("birthDate", birthDate);
parame.putString("city", city);
param.putString("summaryResult", summaryResult);
senderIntent.putExtras(param);
startActivity(senderIntent);
startActivity(intent);
}
});```
Code to receive data:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userRegistration);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String summaryResult =
receiverBundle.
getString
**("summaryResult");**
Toast.makeText(UserRegistration.this, "Receiving the Summary Result: " +summaryResult, Toast.LENGTH_LONG).show();```
you declared startActivity 4 times which is wrong , first you should put all your desired data in the bundle , add the bundle to the intent then start the activity only once. the code should look like this:
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
Bundle sendlerBundler = new Bundle();
sendlerBlundler.putString("summaryResult", summaryResult);
senderIntent.putExtras(sendlerBundler);
startActivity(senderIntent);
}
code in receiving activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userRegistration);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String summaryResult = receiverBundle.getString("summaryResult");
}

How to get value of a TextView which is in another activity?

I want to get the value of a TextView which is defined in another activity? I am using android studio.
You should use intents for passing values to another activities.
in first activity:
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("YOURKEY", yourTextViewValue);
startActivity(intent);
Access that intent on next activity
String textViewValue= getIntent().getStringExtra("YOURKEY");
First activity (which has TextView)
SecondActivity.launchActivity(this, textView.getText().toString())
Second Activity (which need the text value)
public static void launchActivity(Context context, String textViewValue) {
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("textValueKey", textViewValue)
context.startActivity(intent);
}
#Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent() != null) {
String textViewValue = getIntent().getStringExtra("textValueKey");
}
...
}

Passing the selected object to an Another Activity

I have a news activity in which there is a list of news.I want a user to select a news from the list and direct him to the news_details page where I give the details about the selected news, however when the user selects the news, program goes quickly to news_details and comes back again to the news.
News:
public void Listen() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { // ana sayfada herhangi bir item seçildiğinde
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
i.putExtra("title", selectedNews.getTitle());
i.putExtra("date", selectedNews.getNewsDate().toString());
i.putExtra("image_id", selectedNews.getImageId());
i.putExtra("text", selectedNews.getText());
setResult(RESULT_OK, i);
startActivity(i);
}
});
}
News_Details:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news__details);
Intent i = new Intent(this,MainActivity.class);
startActivityForResult(i, GET_NEWS);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GET_NEWS) { // Check which request we're responding to
if (resultCode == RESULT_OK) { // Make sure the request was successful
title.setText(data.getStringExtra("title"));
date.setText(data.getStringExtra("date"));
news_img.setImageResource(data.getIntExtra("image_id", 0));
news_text.setText(data.getStringExtra("text"));
}
}
}
}
First of all, remove this line:
setResult(RESULT_OK, i);
Also, remove this line from newsdetails activity:
startActivityForResult(i, GET_NEWS);
Make changes as below:
public void Listen() {
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
i.putExtra("title", selectedNews.getTitle());
i.putExtra("date", selectedNews.getNewsDate().toString());
i.putExtra("image_id", selectedNews.getImageId());
i.putExtra("text", selectedNews.getText());
startActivity(i);
}
});
}
Then, in your news details activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news__details);
Intent i = getIntent();
String title = i.getStringExtra("title");
String date = i.getStringExtra("date");
int imageId = i.getIntExtra("image_id");
String text = i.getStringExtra("text");
}
OnActivityResult method is not required so simply remove it.
FYI:
startActivity and startActivityForResult both of them start new activities , but startActivityForResult as the name suggests that you are expecting a result from the activity you are starting. And this result shall be obtained in onActivityResult method.
Say for example, you want to start Activity2 from Activity1, and you want to pass some data back to Activity1 while finishing Activity2. You simply set the Result in Activity2 using setResult() method. and while Activity1 resumes again, its onActivityResult() will be invoked, you will override onActivityResult() in Activity1 to receive the Result set by Activity2.
Hope you are now clear on this.
Remove these lines
setResult(RESULT_OK, i);
Intent i = new Intent(this,MainActivity.class);
startActivityForResult(i, GET_NEWS);
Use google Gson for serializing the object.
NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
String strNews = new Gson().toJson(selectedNews);
Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
i.putExtra("news", strNews);
startActivity(i);
On other hand News details onCreate() do this
Bundle bundle = getIntent().getExtras();
String newsStr = bundle.getString("news");
Gson gson = new Gson();
Type type = new TypeToken<NewsItem>() {
}.getType();
NewsItem selectedNews = gson.fromJson(newsStr, type);
to send string value
NewsItem selectedNews = (NewsItem) parent.getItemAtPosition(position);
Intent i=new Intent(MainActivity.this, News_Details_Activity.class);
i.putExtra("title", selectedNews.getTitle());
i.putExtra("date", selectedNews.getNewsDate().toString());
i.putExtra("image_id", selectedNews.getImageId());
i.putExtra("text", selectedNews.getText());
startActivity(i);
to recieve in News_Details_Activity
Intent i = getIntent();
title = i.getStringExtra("title");
date= i.getStringExtra("date");
text= i.getStringExtra("text");
you can do it using Serializable by following way
public class News implements Serializable{
String title;
String desc;
String time,imageUrl;
}
Then News List activity
Intent i = new Intent(MainActivity.this, News_Details_Activity.class);
i.putExtra("news",newsObject);
and get it onCreate of NewsDetail
News news=(News) getIntent().getExtras().getSerializable("news");
and user it like title.setText(news.getTitle());
make News class that implements Serialazable
Create new Object of News class , and put into intent as putSerialazable();
in your second activity just getIntent().getSerialazable("key") and set your data to views.

Multiple Intents in a Single Activity

Having issues with sending multiple pieces of data (in this case, three arrays(two int, one string)) over to a second activity page.
I am unsure of how this is done. What I would like to know is how to send these arrays in one Start Activity method, if that is possible. My current code is:
public void onClickGoToTeamSummary(View view)
{
Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
Intent intentTeamPoints = new Intent(MainActivity.this, ResultsActivity.class);
Intent intentTeamGoals = new Intent(MainActivity.this, ResultsActivity.class);
intentTeamNames.putExtra("footballClubs", myTeams);
intentTeamPoints.putExtra("clubPoints", pointsAttained);
intentTeamGoals.putExtra("clubGoals", goalsScored);
startActivity(intentTeamPoints);
startActivity(intentTeamNames);
startActivity(intentTeamGoals);
}
I had tried:
startActivity(intentTeamPoints, intentTeamNames, intentTeamGoals);
to no avail. To help, my getIntent in the next activity looks like this:
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intentClubNames = getIntent();
String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
Intent intentClubPoints = getIntent();
int[] team_points = intentClubPoints.getIntArrayExtra("clubPoints");
Intent intentTeamGoals = getIntent();
int[] club_goals = intentTeamGoals.getIntArrayExtra("clubGoals");
}
The code itself works provided only one startActivity is used. I would like to know how to pass all my arrays into the second activity page through one activity if anyone can help me.
Try this..
You can send all values in single Intent
public void onClickGoToTeamSummary(View view)
{
Intent intentTeamNames = new Intent(MainActivity.this, ResultsActivity.class);
intentTeamNames.putExtra("footballClubs", myTeams);
intentTeamNames.putExtra("clubPoints", pointsAttained);
intentTeamNames.putExtra("clubGoals", goalsScored);
startActivity(intentTeamNames);
}
and
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_results);
Intent intentClubNames = getIntent();
String[] club_names = intentClubNames.getStringArrayExtra("footballClubs");
int[] team_points = intentClubNames.getIntArrayExtra("clubPoints");
int[] club_goals = intentClubNames.getIntArrayExtra("clubGoals");
}
You don't use multiple Intents but multiple extras.
// create your Intent as normal
Intent myIntent = new Intent(MainActivity.this, ResultsActivity.class);
// then you can add multiple extras
myIntent.putExtra("footballClubs", myTeams);
myIntent.putExtra("clubPoints", pointsAttained);
myIntent.putExtra("clubGoals", goalsScored);
startActivity(myIntent);
Then receiving them would be the same. You would just receive the one Intent and use the key as normal for each extra Array.

Android Getting Results from another activity

I have a problem with my code. I want to pass a String from the SecondActivity to FirstActvity. Note that the FirstActivity is not visible but its still open. when the SecondActivity is finish it passes a String to the FirstActivity.
My problem here is that when the SecondActivity ended and goes to FirstActivity, the whole application closes.
FirstActivity to SecondActivity:
Intent intent = new Intent(MainActivity.this, FileChooser.class);
startActivityForResult(intent, 0);
SecondActivity to FirstActivity:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("filePath", "/sdcard/path1");
setResult(0);
finish();
FirstActivity Result:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//TODO handle here.
Intent intent = getIntent();
this.filePath = intent.getExtras().getString("filePath");
}
What is wrong with the code?
When you set the result of your SecondActivity, you only set the result code. Instead of setResult(0) use setResult(0,intent)
Also, in your FirstActivity's onActivityResult get the extra from the data argument - this.filePath = data.getExtras().getString("filePath");
Try to use
data.getExtras().getString("filePath");
instead of
intent.getExtras().getString("filePath");`
Try with Bundle:
First Activity;
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), FIRSTACTIVITY.class);
Bundle bundle = new Bundle();
bundle.putString("filePath","/sdcard/path1");
intent.putExtras(bundle);
startActivity(intent);
}
Second Activity:
public void activity_value() {
Intent i = getIntent();
Bundle extras=i.getExtras();
if(extras !=null) {
value = extras.getString("filePath");
}
}
try this
example.It solves your problem.

Categories

Resources