This question already has answers here:
How to pass a value from one Activity to another in Android?
(7 answers)
Passing ArrayList through Intent
(6 answers)
How do I pass data between Activities in Android application?
(53 answers)
Closed 5 years ago.
I am developing a QR Code app for android. I want to pass whatever I scan into a new activity. This is my QR Code results code,
public void handleResult(final Result result)
{
final String scanResult = result.getText();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Scanned Result");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialogInterface, int i) {
scannerView.resumeCameraPreview(MainActivity.this);
}
});
builder.setNeutralButton ("Show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
startActivity(new Intent(MainActivity.this, ResultPage.class));
}
});
builder.setMessage(scanResult);
AlertDialog alert = builder.create();
alert.show();
}
ResultPage.class is the activity I wish to display the scanned result on to, how do I do this? Edit: My ResultPage.Class is currently empty with only an empty textview. I'd like the output to be in the textview.
final String scanResult = result.getText();
Intent intent = new Intent(getBaseContext(), ResultPage.class);
intent.putExtra("SCAN_RESULT", scanResult);
startActivity(intent);
Now you can find scanResult in ResultPage activity
String s = getIntent().getStringExtra("SCAN_RESULT");
If you have URI then pass that URI using Intent
if you want to pass image itself
Bitmap image= imageView.getDrawingCache();
Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);
and in another activity receive like:
Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
image.setImageBitmap(bmp );
You will have to pass the Resulted string from MainActivity to ResultPage.
For passing the data from one Activity to Another Activity, you can use Bundle.
Here is the code for sending data to another Activity:
builder.setNeutralButton ("Show", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i)
{
Intent intent = new Intent(MainActivity.this,ResultPage.class);
Bundle bundle = new Bundle();
bundle.putString("RESULT",scanResult);
intent.putExtra(bundle);
startActivity(intent);
}
});
And now you can get this result to ResultPage Activity.
Here is the code for getting data.
Bundle bundle = getIntent().getExtras();
String scanResult = bundle.getString("RESULT");
Now you can Display your scanResult.
Related
my Onbindview holder
Glide.with(holder.t1.getContext())
.load("http://example/example/images/" +data.get(position).getImage()).into(holder.img);
}
And my interface
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (iOtobusSaatleriInterface != null){
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION);
iOtobusSaatleriInterface.onItemClick(position);
Intent intent = new Intent(context,deneme.class);
intent.putExtra("name",data.get(position).getName());
intent.putExtra("resim", String.valueOf(Glide.with(itemView)
.load("http://example/example/images/")));
context.startActivity(intent);
}
}
});
my new activity
Intent intent = getIntent();
String name = intent.getStringExtra("name");
int goruntu = intent.getIntExtra("resim",0);
imageView.setImageResource(goruntu);
textView.setText(name);
finally my photo is not coming. I just can't get the image data in the new activity. This data is registered in mysql and I am pulling from the directory with retrofit.
New display
my imageview display
And my xml
You can pass the image as a string from screen A to screen B.
The batter way is to pass full URL in intent as String and receive it on another Activity.
Intent intent = new Intent(context,deneme.class);
intent.putExtra("name",data.get(position).getName());
intent.putExtra("resim","YOUR_FULL_URL");
At Another Activity
Intent intent = getIntent();
String name = intent.getStringExtra("name");
String goruntu = intent.getStringExtra("resim");
String.valueOf(Glide.with(imageView)
.load(goruntu)
textView.setText(name);
Without knowing more about why you are trying to send a raster image over IPC between activities, I think the best advice I can give you is to not try to do that.
Instead, simply send the URL in the intent and have Activity 2 use glide to load and display the image.
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);
I'm trying to make a dialogue box with a positive bottom that changes the activity from SignupAcitvity to MainActivity. So I used the intent method to do this. However, once I tried this, I got the following error message:Cannot Resolve constructor 'Intent(com.androidcodefinder.loginscreendemo.Profile.ExampleDialogue, java.lang.Class<com.androidcodefinder.loginscreendemo.MainActivity>)'. Can you help me fix this?
My code:
public class ExampleDialogue extends AppCompatDialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialayout,null);
builder.setView(view)
.setTitle("Confirm Your Email")
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
})
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(SignUpActivity.class, MainActivity.class);
startActivity(intent);
}
});
return builder.create();
}
}
The reason that you are getting this error is because you are in the ExampleDialogue activity, but you are starting an intent from SignUpActivity.class to MainActivity.class. If you want to go to MainActivity.class, you will have to do:
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
Also, it can't resolve the constructor so just add this default constructor, which you can change as you wish:
public ExampleDialogue(){
//Constructor code.
}
You are trying
Intent intent = new Intent(SignUpActivity.class, MainActivity.class)
However, Intent has no constructor with 2 classes as arguments. Hence the error.
What you want, is to use the constructor Intent(Context, Class<?>). Specifially, in your case you do this:
Intent intent = new Intent(getActivity(), MainActivity.class);
I am trying to send an Intent value between two activities, though it appears, having read this, that in my second activity, the received intent is null; having encoutnered an NPE at runtime.
The intended functionality behind this is: 'the user scans a code in Activity A' -> 'a true value received and packed into an intent' -> 'Activity B opens, unpacks the intent and checks that the intent value is true' -> 'if true, the height of an ImageView in the activity is reduced by a set amount'.
I am therefore, not sure why my Intent is received as null in Activity B, as I would like this check to happen so that the height is updated when the activity opens?
Activity A:
//do the handling of the scanned code being true and display
//a dialog message on screen to confirm this
#Override
public void handleResult(Result result) {
final String myResult = result.getText();
Log.d("QRCodeScanner", result.getText());
Log.d("QRCodeScanner", result.getBarcodeFormat().toString());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Activity Complete!");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
//the user has pressed the OK button
#Override
public void onClick(DialogInterface dialog, int which) {
scannerView.resumeCameraPreview(QRActivity.this);
//pack the intent and open our Activity B
Intent intent = new Intent(QRActivity.this, ActivityTank.class);
intent.putExtra("QRMethod", "readComplete");
startActivity(intent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
}
Activity B:
// in onCreate, I check that the bundled intent is true
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tank);
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if (extras == null) {
//then the extras bundle is null, and nothing needs to be called here
}
else {
String method = extras.getString("QRmethod");
if (method.equals("readComplete")) {
updateTank();
}
}
}
}
//this is the method that is called when the intent check is true
public int tHeight = 350;
public void updateTank() {
ImageView tankH = (ImageView)findViewById(R.id.tankHeight);
ViewGroup.LayoutParams params = tankH.getLayoutParams();
params.height = tHeight - 35;
tankH.setLayoutParams(params);
}
In Activity B you have a typo while pulling QRMethod from the Intent extras. You are using QRmethod while you have set extras with 'QRMethod'.
You can use :
In first activity ( MainActivity page )
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("QRmethod","readComplete" );
then you can get it from your second activity by :
In second activity ( SecondActivity page )
Intent intent = getIntent();
String YourtransferredData = intent.getExtras().getString("QRmethod");
You can get string value by using intent.getStringExtra() method like this in your second activity.
if (getIntent() != null){
getIntent().getStringExtra("QRmethod") //this s your "readComplete" value
}
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.