Problem sending data to other activity in Android Studio - java

I want to send data from three activities to the last activity, how can I do
1. First Activity
else {
//first activity want transfer data last activity
Intent intent = new Intent(Register.this, Register2.class);
intent.putExtra("name", reg_name);
intent.putExtra("surname", reg_surname);
intent.putExtra("email", reg_email);
startActivity(intent);
}
2. Second Activity
else {
//second activity transfer data last activity
Intent intent = new Intent(Register2.this, PhoneNumberOtp.class);
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
}
3. Third Activity
else {
//third activity transfer data last activity
Intent intent = new Intent(PhoneNumberOtp.this, OTPVerification.class);
intent.putExtra("phoneNumber", reg_phone_number);
startActivity(intent);
}
Last Activity
else {
Intent intent = new Intent(OTPVerification.this, HomeActivity.class);
//get data register activity
String name = getIntent().getExtras().getString("name");
String surname = getIntent().getExtras().getString("surname");
String email = getIntent().getExtras().getString("email");
String username = getIntent().getExtras().getString("username");
String password = getIntent().getExtras().getString("password");

Extras you put on an intent are not automatically passed along to subsequent activities after the first one they are sent to directly. If you have some data you want to pass through multiple activities, you have to re-add it to the new sending intent for each step. For example:
First
Intent intent = new Intent(UserActivity.this, AccountActivity.class);
intent.putExtra("name", reg_name);
intent.putExtra("email", reg_email);
startActivity(intent);
Second
Intent intent = new Intent(AccountActivity.this, RegisterActivity.class);
// get the received data and add it to the new intent
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
intent.putExtra("name", name);
String email = recv.getStringExtra("email");
intent.putExtra("email", email);
}
// then add any new data
intent.putExtra("username", reg_username);
intent.putExtra("password", reg_password);
startActivity(intent);
Third
// RegisterActivity now has 4 strings available
Intent recv = getIntent();
if( recv != null ) {
String name = recv.getStringExtra("name");
String email = recv.getStringExtra("email");
String username = recv.getStringExtra("username");
String password = recv.getStringExtra("password");
}
In the example you posted, the last activity would only have access to phoneNumber, since that's all you passed it from the third activity.

Related

How do I fix "startActivity() in ContextCompat cannot be applied to?"

I am using Google Vision OCR to grab the email from a business card (the OCR Graphic activity) and send it to the the To destination in the SendEmail activity. My log shows that the email text is detected.
I tried to set the intent to send it to the next activity, but I am getting two errors, "cannot resolve constructor Intent" on my new intent, and start activity cannot be applied to.
This is the OcrGraphic activity
List<Line> lines = (List<Line>) text.getComponents();
for(Line elements : lines) {
float left = translateX(elements.getBoundingBox().left);
float bottom = translateY(elements.getBoundingBox().bottom);
if (elements != null && elements.getValue() != null) {
if (elements.getValue().matches("^[_A-Za-z0-9-\\\\+]+(\\\\.[_A-Za-z0-9-]+)*#\"\n" +
"\t\t+ \"[A-Za-z0-9-]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$") || elements.getValue().contains("#")) {
Log.e("elementsemail", elements.getValue());
String email;
email = elements.getValue();
cEmail = email;
Intent sendIntent = new Intent(this, SendEmail.class);
sendIntent.putExtra(Intent.EXTRA_EMAIL, cEmail);
startActivity(sendIntent);
}
this is my Send Email activity
private void sendMail(){
Intent getIntent = getIntent();
String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);;
String[] recipients = recipientList.split(",");
String subject = mEditTextSubject.getText().toString();
String message = mEditTextMessage.getText().toString();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setType("message/rfc822");
startActivity(Intent.createChooser(intent, "Choose an email client"));
}
I want to send the email address to the SendEmail activity. I am new to java and android, any help is welcomed.
I think you're problem is how you obtain the extra(EXTRA_EMAIL)
Replace String recipientList = getIntent.getStringExtra(OcrGraphic.cEmail);; with String recipientList = getIntent.getStringExtra(Intent.EXTRA_EMAIL);
please replace this :
Intent sendIntent = new Intent(this, SendEmail.class);
whit this :
Intent sendIntent = new Intent(getApplicationContext(), SendEmail.class);
edit
You need to pass context in a constructor like this
private Context context;
OcrGraphic(GraphicOverlay overlay, TextBlock text, Context context) {
super(overlay);
this.context = context;
}
And then
Intent sendIntent = new Intent(context, SendEmail.class);

Checking if user came from intent

I am wondering how I can accomplish checking if an activity was started with an intent.
What I have tried:
I have tried checking if the object was null, but due to my setup, I cannot check that. I have also tried executing with a code, but that failed too.
My code:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("Title", one);
intent.putExtra("Description", two);
/////////////////////////
Intent intent = getIntent();
String title = intent.getStringExtra("Title");
String description = intent.getStringExtra("Description");
Many thanks!
All activities are started with an Intent,
But we can check is Intent has detail in bundle or not?
like in your case -
Intent intent = getIntent();
if( intent!= null && intent.getExtras() != null
&& !intent.getExtras().getString("Title").equals("")
&& !intent.getExtras().getString("Description").equals("") ) {
// Activity started with sending title & description
}
else {
// Activity started without sending title & description
}
just replace this line.
Intent intent = getIntent();
To
Intent intent = getIntent().getExtra();
you forgot to add .getExtra().

I want to send integer,String,Object,double,Bitmap,HashMap from one activity to another in android

I am begnning in android , I want to send String,Object,double,ArrayList from one activity to another . I already done with integer but when i want to send other data like object and ArrayList i am facing problem.
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("intVariableName", intValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
int intValue = mIntent.getIntExtra("intVariableName", 0);
Passing double data:
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("doubleVariableName", doubleValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
double doubleValue = mIntent.getDoubleExtra("doubleVariableName", 0.00); // set 0.00 as the default value if no value for doubleVariableName found
Passing String data:
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("stringVariableName", stringValue);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
String stringValue = mIntent.getExtras().getString("stringVariableName");
or
Intent mIntent = getIntent();
String stringValue = mIntent.getStringExtra("stringVariableName");
Passing ArrayList data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putStringArrayListExtra("arrayListVariableName", arrayList);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
arrayList = mIntent.getStringArrayListExtra("arrayListVariableName");
Passing Object data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("ObjectVariableName", yourObject);
startActivity(myIntent);
ReceiverActivity
Intent mIntent = getIntent();
yourObj = mIntent.getSerializableExtra("ObjectVariableName");
Note : Keep in mind your custom Class must implement the Serializable interface.
Passing HashMap data :
SenderActivity
HashMap<String, String> hashMap;
Intent mIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
mIntent.putExtra("hashMap", hashMap);
startActivity(mIntent);
ReceiverActivity
Intent mIntent = getIntent();
HashMap<String, String> hashMap = (HashMap<String, String>)
mIntent.getSerializableExtra("hashMap");
Passing Bitmap data :
SenderActivity
Intent myIntent = new Intent(SenderActivity.this, ReceiverActivity.class);
myIntent.putExtra("image",bitmap);
startActivity(mIntent);
ReceiverActivity
Intent mIntent = getIntent();
Bitmap bitmap = mIntent.getParcelableExtra("image");
You can send object one activity to another by using Serializable class
check following example:
http://hmkcode.com/android-passing-java-object-another-activity/
http://www.technotalkative.com/android-send-object-from-one-activity-to-another-activity/
you can send like:
Intent intent = new Intent(mcontext, activity.class);
intent .putExtra("Key",product);
startActivity(intent );
and get value like:
Intent intent = getIntent();
Bundle bundle = intent.getExtras();`enter code here`
product = (ClassName) bundle.getSerializable("Key");
You can make a bean of the data you want to send like this
public class YourBean implements Serializable {
String yourString;
Object yourObject;
double yourDouble;
ArrayList<String> yourList;
public String getYourString() {
return yourString;
}
public void setYourString(String yourString) {
this.yourString = yourString;
}
public Object getYourObject() {
return yourObject;
}
public void setYourObject(Object yourObject) {
this.yourObject = yourObject;
}
public double getYourDouble() {
return yourDouble;
}
public void setYourDouble(double yourDouble) {
this.yourDouble = yourDouble;
}
public ArrayList<String> getYourList() {
return yourList;
}
public void setYourList(ArrayList<String> yourList) {
this.yourList = yourList;
}
}
then when you want to pass data in intent do like this
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
YourBean yourBean = new YourBean();
yourBean.setYourString("your string");
yourBean.setYourDouble(Your double);
yourBean.setYourObject(Your Object);
yourBean.setYourList(array list);
intent.putExtra("bean",yourBean);
startActivity(intent);
then you can get it like this in your SecondActivity like this
YourBean yourBean1 = (YourBean) getIntent().getSerializableExtra("bean");
You can use intents. In a intent you can put all sort of data, String, int, whatever you want.
In your case, in activity1, before going to activity2, you will store a String message this way :
Intent intent = new Intent(activity1.this, activity2.class);
intent.putExtra("message", message);
startActivity(intent);
In activity2, in onCreate(), you can get the String message by retrieving a Bundle (which contains all the messages sent by the calling activity) and call getString() on it :
Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");
Then you can set the text in the TextView or where you want
Hope this helps you !
Intent mIntent = new Intent(MainActivity.this, AboutActivity.class);
ArrayList<String> mArray = new ArrayList<>();
mArray.add("Mon");
mArray.add("Tue");
mIntent.putStringArrayListExtra("Array", mArray);
startActivity(mIntent);
// In AboutActivity.java recieved data
ArrayList<String> mArray= getIntent().getExtras().getStringArrayList("Array");
Toast.makeText(AboutActivity.this, ""+mArray, Toast.LENGTH_LONG).show();

What is the difference between getIntent() and new Intent() in Android?

I do like this
first way
Intent intent = new Intent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
second way
Intent intent = getIntent();
intent.putExtra("isLoggedIn",true);
setResult(RESULT_OK,intent);
Both can give the same result.I want to know the actual difference between this two
In the context of an Activity, getIntent() will return the Intent that was originally sent to the Activity. The example you gave may work the same, but you should really avoid using getIntent() if you are passing the Intent to another Activity or sending it back as a result.
For example:
If I start an activity with:
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra("key", "test");
startActivity(intent);
Then in my MainActivity class:
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
So now consider if you have SecondActivity and I am starting it from MainActivity using getInent();
Intent intent = getIntent();
intent.setClassName("com.example.pkg", "com.example.pkg.SecondActivity"");
intent.setComponent(new ComponentName("com.example.pkg", "com.example.pkg.SecondActivity"));
intent.putExtra("isLoggedIn",true);
startActivity(intent);
Then in my SecondActivity I can access key and isLoggedIn both.
Intent intent = getIntent();
String value = intent.getString("key"); // value will = "test".
boolean testIsLoggedIn = intent.getBooleanExtra("isLoggedIn",true);
So, generally it is not good practice to use the getIntent to start further activities.

Passing data between 2 activities

I am programming in android for a Samsung tablet, and I have 2 activities, one of this is a list of football teams, and the other activity is their twitter, but i have a problem to pass the parameter for the first activity to the second one. I want to pass their url like a string, but i cant get it.
Thanks!
You have to use Intent:
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("extraURL", "http://myUrl.com");
startActivity(i);
Then, to retrieve it in SecondActivity, in the onCreate method do:
Intent receivedIntent = getIntent();
String myUrl = receivedIntent.getStringExtra("extraURL");
You typically launch the 2nd Activity from the first using an Intent. You can pass data to the 2nd Activity using the same Intent you use to launch it. For example,
Intent i = new Intent(this, SecondActivity.class);
i.putExtra("url", "http://url.you.want.to.pass/");
startActivity(i);
In the 2nd Activity, in the onCreate, you can read the data using:
Intent i = getIntent();
String url = i.getStringExtra("url");
your First Activity on click of button
Intent intent = new Intent(this,ActivityTwo.class );
intent.setAction(intent.ACTION_SEND);
intent.putExtra("www.google.com",true);
startActivity(intent);
Receive it in ActivityTwo:-
Intent intent = getIntent();
String msg = intent.getStringExtra("www.google.com");

Categories