I'm still learning AS & java and recently bought an app code but it has "getArguments()" inside of fragment which I'd like to convert it into activity. Help is appreciated.
This is the code :
String weburl = getArguments().getStringArray(MainActivity.FRAGMENT_DATA)[0];
String data = getArguments().containsKey(LOAD_DATA) ? getArguments().getString(LOAD_DATA) : null;
if (data != null) {
browser.loadDataWithBaseURL(weburl, data, "text/html", "UTF-8", "");
} else {
browser.loadUrl(weburl);
}
How do I write the same code in an activity?
I think what you are looking for is how to send info to an Activity, for that you need Intents
Please verify this info: https://developer.android.com/reference/android/content/Intent
Basically:
public static final String EXTRA_MESSAGE = "extra message";
...
Intent intent = new Intent(this, SecondActivity.class);
String message = "Hello this is an intent";
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
To retrieve that data you have to do:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
I invite you to check this out: https://developer.android.com/training/basics/firstapp/starting-activity#java
Intent extras are the activity equivalent of fragment arguments.
Related
Please am designing an android app, i want to send (5)String values from one activity to another activity to use in different TextViews, i have tried virtually all the code i could find online on the topic, but i keep getting just one value(the last value i send in the putExtra()). please i am new to Android Studio and will appreciate every help.
I have used the putExtra() to send one data to another activity and it worked perfectly, while trying to do the same with multiple data i keep getting just one of the data sent.
I have also tried using a bundle object, to receive the data from the other(recieving) activity.
I expect getting all this data ( intent.putExtra("surname", "Jerry").
intent.putExtra("middlename", "chris"). intent.putExtra("lastname", "Enema")) in another activity, but i keep getting just "Enema" alone
this is my code;
//in the firstActivity
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String sFirstname = firstname.getText().toString();
String sLastname = lastname.getText().toString();
Intent intent = new Intent(MainActivity.this, ReceiveActivity.class);
intent.putExtra("surname" ,sFirstname);
intent.putExtra("lastname", sLastname);
startActivity(intent);
}
});
//And In the second Activity
firstname = findViewById(R.id.firstname);
lastname = findViewById(R.id.firstname);
Intent intent = getIntent();
Bundle bundle = getIntent().getExtras();
String ssurname = bundle.getString("surname");
String slastname = bundle.getString("lastname");
firstname.setText(ssurname);
lastname.setText(slastname);
Try this.
In First Activity:
String sFirstname = "Tope";
String sLastname = "Adebodun";
Intent theIntent = new Intent(MainActivity.this, ReceivingActivity.class);
theIntent.putExtra("firstname", sFirstname);
theIntent.putExtra("lastname", sLastname);
Then in your second activity, do this in the onCreate method:
Intent intent = getIntent();
String thefirst = (String) intent.getExtras.getString("firstname");
String thelast = (String) intent.getExtras.getString("lastname");
I would prefer not to ue getExtras so you should have two getExtra
Like this :
Intent intent = getIntent();
String ssurname = intent.getExtra("surname");
String slastname = intent.getExtra("lastname");
firstname.setText(ssurname);
lastname.setText(slastname);
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);
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'm trying to use startActivityForResult() to get a String from another activity, but I keep getting a NullPointerException whenever I try to retrieve the String from the Intent. Here's what I've got:
//To set up the Intent:
String in = mEditText.getText().toString(); //medittext is EditText that I want String from
Intent i = new Intent(this, ActivityLoaderActivity.class); //activity that started this one
i.putExtra("message", in);
setResult(1);
this.finish(); //cause I'm using startActivityForResult()
//To get the String
#Override
protected void onActivityResult( ... , Intent data) {
String s = data.getStringExtra("message"); //error here
}
I know the error is at getStringExtra() through debugging, but I still can't figure out why it's crashing. Anyone have any ideas?
Maybe you could try using setResult(1, i) and checking in onActivityResult() for the resultCode before getting the extra like this
if(resultCode == 1)
{
//get String extra
}
Hope it helps
I am trying to get the entered data from an activity. From my main screen, I kick off the activity like this:
Intent myIntent = new Intent(this, ContactInfo.class);
startActivityForResult(myIntent, AppState.ACTIVITY_CONTACT_INFO);
In the activity, upon the user tapping the Save button, I fire off the following:
Intent intent = new Intent();
TextView tvName = (TextView) findViewById(R.id.txtContactName);
intent.putExtra("Name", tvName.getText());
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK, intent);
}
finish();
In the original activity, I catch the onActivityResult event like this:
String contactName = (String) data.getExtras().get("Name");
However, this line blows up with java.lang.ClassCastException: android.text.SpannableString cannot be cast to java.lang.String. I've also tried getStringExtra with same results.
What am I missing?
TextView.getText() doesn't return a string, but rather the SpannableString you see. Use getText().toString() instead.
Had a similar problem, where the error appear, android.text.SpannableString cannot be cast to java.lang.String
It was because getText() needs to have a 'toString()' at the end. Without this 'toString()', it will crash on Android 4.x, but will work on Android 2.x.
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
TextView viewTxt; // Continue and complete this yourself.
String tmpStr = (String) viewTxt.getText().toString();