I'm asking for text input on Wear OS using the RemoteInput intent, with the following code:
public class MainActivity extends Activity {
private ActivityMainBinding binding;
private static final int MESSAGE_INPUT_RESULT = 102;
private static final String RESULT_KEY = "input_result";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
binding.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Prepare the input request
Intent remoteInputIntent = new Intent("android.support.wearable.input.action.REMOTE_INPUT");
RemoteInput[] remoteInputArray = new RemoteInput[1];
remoteInputArray[0] = new RemoteInput.Builder(RESULT_KEY)
.setAllowFreeFormInput(true)
.setLabel("Your message to Bob")
.setChoices(new CharSequence[] {"OK", "Thanks"})
.build();
// TODO: this DOESN'T WORK, I need to pass an additional value
int myValue = 55;
remoteInputIntent.putExtra("myValue", myValue);
// TODO:
// Display the input request
remoteInputIntent.putExtra("android.support.wearable.input.extra.REMOTE_INPUTS", remoteInputArray);
startActivityForResult(remoteInputIntent, MESSAGE_INPUT_RESULT);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MESSAGE_INPUT_RESULT && resultCode == RESULT_OK) {
Bundle bundle = RemoteInput.getResultsFromIntent(data);
if (bundle != null) {
// TODO: NONE of the two statements below work!
int myValue = data.getIntExtra("myValue", 0);
//int myValue = bundle.getInt("myValue");
// Get the text the user typed
CharSequence charSequence = bundle.getCharSequence(RESULT_KEY);
if (charSequence != null && charSequence.length() > 0)
// Display the text and the value in a toast
Toast.makeText(this, charSequence + ", myValue = " + String.valueOf(myValue), Toast.LENGTH_SHORT).show();
}
}
}
}
However, in the toast that is displayed, myValue is always 0. How can I pass additional data using the same intent, so that I will be able to read it in the onActivityResult routine and take appropriate actions?
Related
I recently started Android Development, I wanted to create a form by which I also take user address. In the address field I placed a button to choose their location by using map, I successfully get location but the problem is when I return back to main activity all my fields become empty such as name,phone,email e.t.c.
I want that all my fields remain filled after selecting map location
Here is my Main Activity
public class MainActivity extends AppCompatActivity {
private EditText Name, Phone, Destination;
private Button mBtnAdd;
private String type = "1";
TextView textView;
LocationManager locationManager;
String lattitude,longitude;
private static final int REQUEST_LOCATION = 1;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Name = (EditText) findViewById(R.id.name);
Phone = (EditText) findViewById(R.id.phone);
Destination = (EditText) findViewById(R.id.destinationAddress);
Intent i = getIntent();
String address = i.getStringExtra ( "address");
Destination.setText(address);
}
private void pickup() {
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
intent.putExtra ( "map_type", "1" );
startActivity(intent);
}
}
Here Is my select location Function in main Map Activity
public void updateLocation(View view) throws IOException {
final EditText location = (EditText) findViewById(R.id.name);
LatLng center = mMap.getCameraPosition().target;
Log.e(TAG, "Center: " + center);
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(center.latitude, center.longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
Log.e(TAG, "addresses: " + addresses);
String address = addresses.get(0).getAddressLine(0);
Intent i = getIntent();
Intent intent = new Intent( MapsActivity.this, MainActivity.class );
intent.putExtra ( "address", address );
startActivity(intent);
}
you need to implement methods onSaveInstanceState and onRestoreInstanceState in your MainActivity:
#Override
protected void onSaveInstanceState(Bundle state) {
/* putting the current instance state into a Bundle */
state.putString(InstanceStateKeys.CURRENT_NAME, this.Name.getText());
state.putString(InstanceStateKeys.CURRENT_PHONE, this.Phone.getText());
state.putString(InstanceStateKeys.CURRENT_DEST, this.Destination.getText());
super.onSaveInstanceState(state);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
/* and then obtain these values from the Bundle again */
String currentName = state.getString(InstanceStateKeys.CURRENT_NAME);
String currentPhone = state.getString(InstanceStateKeys.CURRENT_PHONE);
String currentDest = state.getString(InstanceStateKeys.CURRENT_DEST);
/* in order to update the GUI with these values */
if(currentName != null) {this.Name.setText(currentName);}
if(currentPhone != null) {this.Phone.setText(currentPhone);}
if(currentDest != null) {this.Destination.setText(currentDest);}
}
where InstanceStateKeys is just a class with some String constants, for example:
public class InstanceStateKeys {
public static final String CURRENT_NAME = "name";
public static final String CURRENT_PHONE = "phone";
public static final String CURRENT_DEST = "dest";
}
while actually, you should better use .startActivityForResult() to start that MapsActivity and then handle the result within the MainActivity's implementation of .onActivityResult() ...eg. in order to pass back the destination address.
put together it might look alike this ...
/** start the {#link MapsActivity} for a result: */
private void startMapsActivity() {
Intent intent = new Intent(this.getContext(), MapsActivity.class);
intent.putExtra ("map_type", 1);
this.startActivityForResult(intent, RequestCodes.REQUESTCODE_PICK_DESTINATION);
}
then, in order to return that result to the MainActivity:
public void updateLocation(View view) throws IOException {
...
Intent result = new Intent();
result.putExtra ("address", address);
this.setResult(Activity.RESULT_OK, result);
this.finishActivity(RequestCodes.REQUESTCODE_PICK_DESTINATION);
}
finally, when having returned to the MainActivity:
/** handle the result: */
#Override
public void onActivityResult(int requestCode, int resultCode, Intent result) {
String address = null;
if(resultCode == Activity.RESULT_OK && result != null) {
extras = result.getExtras();
if(extras != null) {
address = extras.getString("address", null);
Log.d(LOG_TAG, "the received address is: " + address);
}
}
}
the SDK documentation explains it pretty well: Understand the Activity Lifecycle.
I am trying to add an image from the gallery to an image view. It has been added but the image is disappearing from the image view on rotating the screen. The image has been saved to firebase. How to save the activity state? I have searched for it but did not find the relevant answer.Can anyone help me out to solve the problem.
public class skcreateac extends AppCompatActivity
{
static final int PICK_IMAGE_REQUEST = 1;
private EditText sketac1;
private EditText sksetac1;
private EditText sketac2;
private EditText sketac3;
private EditText sketac4;
private EditText sketac5;
private Button skbt;
private Spinner spac;
private ImageView skimg;
private StorageReference skdbimg, fp;
private Firebase skrootac1;
private String strsk, skurl;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_skcreateac);
spac = (Spinner)findViewById(R.id.spinner2);
String cities[] = new String[]{"Hyderabad", "Warangal"};
ArrayAdapter<String> cityadapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, cities);
spac.setAdapter(cityadapter);
skimg = (ImageView) findViewById(R.id.skimg);
skdbimg = FirebaseStorage.getInstance().getReference();
sketac1 = (EditText) findViewById(R.id.skonac);
sksetac1 = (EditText) findViewById(R.id.sksncac);
sketac2 = (EditText) findViewById(R.id.skphcac);
sketac3 = (EditText) findViewById(R.id.sksaddress);
sketac4 = (EditText) findViewById(R.id.skpwcac);
sketac5 = (EditText) findViewById(R.id.skpwrcac);
skbt = (Button) findViewById(R.id.bsu);
strsk = getIntent().getExtras().getString("value");
String url1 = "https://my-app2-a14eb.firebaseio.com/Shopkeepers/";
String url2 = strsk;
skurl = url1 + url2;
skrootac1 = new Firebase(skurl);
skimg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent skimg = new Intent(Intent.ACTION_PICK);
skimg.setType("image/*");
startActivityForResult(skimg, PICK_IMAGE_REQUEST);
}
});
skbt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s1 = sketac1.getText().toString();
if (sketac1.getText().length() == 0) {
sketac1.setError("Kindly enter your Name");
}
Firebase childac1 = skrootac1.child("Name");
childac1.setValue(s1);
String ss1 = sksetac1.getText().toString();
if (sksetac1.getText().length() == 0) {
sksetac1.setError("Kindly enter your ShopName");
}
Firebase childsac1 = skrootac1.child("ShopName");
childsac1.setValue(ss1);
String s2 = sketac2.getText().toString();
long i = Long.parseLong(s2);
int j = 0;
while (i > 0) {
i = i / 10;
j++;
}
if (j == 10) {
Firebase childac2 = skrootac1.child("Phone");
childac2.setValue(s2);
} else {
Toast.makeText(skcreateac.this, "Enter valid Mobile number", Toast.LENGTH_LONG).show();
}
String s3 = spac.getSelectedItem().toString();
Firebase childac3 = skrootac1.child("City");
childac3.setValue(s3);
String s4 = sketac3.getText().toString();
Firebase childac4 = skrootac1.child("Address");
childac4.setValue(s4);
String s5 = sketac4.getText().toString();
String s6 = sketac5.getText().toString();
if (s5.equals(s6)) {
Firebase childac5 = skrootac1.child("Password");
childac5.setValue(s5);
} else {
Toast.makeText(skcreateac.this, "Confirm Password field doesnot match with Create Password field", Toast.LENGTH_LONG).show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
final Uri uri = data.getData();
fp = skdbimg.child("SKPhotos").child(strsk);
fp.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Picasso.get().load(uri).fit().centerCrop().into(skimg);
Toast.makeText(skcreateac.this, "Uploaded photo", Toast.LENGTH_LONG).show();
}
});
}
}
Add following -
#Override
protected void onSaveInstanceState (Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("outputFileUri", photoURI);
}
Make a private URI photoURI = null; in your activity.
Then in your onCreate() add do
#Override
protected void onCreate(Bundle savedInstanceState){
// your other codes
skrootac1 = new Firebase(skurl);
// your other codes
if (savedInstanceState != null)
{
photoURI= savedInstanceState.getParcelable("outputFileUri");
setImage(photoURI);
}
}
private void setImage(final Uri uri){
fp = skdbimg.child("SKPhotos").child(strsk);
fp.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Picasso.get().load(uri).fit().centerCrop().into(skimg);
Toast.makeText(skcreateac.this, "Uploaded photo", Toast.LENGTH_LONG).show();
}
});
}
And finally, change your onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK) {
photoURI = data.getData();
setImage(photoURI);
}
}
As i am developing an android app and wants to convert speech into text, i am using built-in Google speech input activity to convert voice into text. I need past information but it continuously get cleared i got only current response. How need to handle same as google voice keyboard. As i talk it included to current String instep of clear.
MainActivity .java
public class MainActivity extends AppCompatActivity
{
private EditText txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSpeechInput = findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
promptSpeechInput();
}
});
private void promptSpeechInput()
{
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);
try
{
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
}
catch (ActivityNotFoundException a)
{
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case REQ_CODE_SPEECH_INPUT:
{
if (resultCode == RESULT_OK && null != data)
{
final ArrayList<String> result= data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
}
break;
}
}
}
If you only want to save one previously detected String, for this to achieve, you need to make a global String variable and store the value in that variable from results list.(Save the same String as you are setting on text view). But if you want to save all the strings, you need to make global String Arraylist and add all those string in that array list. Below is the code for that.
private EditText txtSpeechInput;
private ImageButton btnSpeak;
private final int REQ_CODE_SPEECH_INPUT = 100;
private List<String> previousStringList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
previousStringList = new ArrayList<>();
txtSpeechInput = findViewById(R.id.txtSpeechInput);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
});
}
private void promptSpeechInput() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault());
intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
getString(R.string.speech_prompt));
intent.putExtra(RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000);
try {
startActivityForResult(intent, REQ_CODE_SPEECH_INPUT);
} catch (ActivityNotFoundException a) {
Toast.makeText(getApplicationContext(),
getString(R.string.speech_not_supported),
Toast.LENGTH_SHORT).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
final ArrayList<String> result = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtSpeechInput.setText(result.get(0));
if (result.get(0) != null) {
previousStringList.add(result.get(0));
}
}
break;
}
}
}
Hope that helps you.If you don't understand anything feel free to ask. If you don't want to save same String twice(already saved string), just replace below conditional line of code..
if (result.get(0) != null && !previousStringList.contains(result.get(0))) {
previousStringList.add(result.get(0));
}
The words get stored in Arraylists.
You can see an example of the implementation here, it works fine. The app stores the words, and then also performs the action requested.
https://github.com/saumyabahu/Travel-Safe/blob/master/MainActivity.java
public class MainActivity extends AppCompatActivity {
private SpeechRecognizer speechRecognizer;
private Intent intentRecognizer;
private EditText txtSpeechInput;
private ImageButton btnSpeak;
//this is the string in which words get stored and past strings with left to right cursor.
String previous = " ";
// ArrayList result = null;
private final int REQ_CODE_SPEECH_INPUT = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_main );
// ActivityCompat.requestPermissions( this, new String[]{Manifest.permission.RECORD_AUDIO}, PackageManager.PERMISSION_GRANTED );
txtSpeechInput = findViewById( R.id.ed );
btnSpeak = (ImageButton) findViewById( R.id.iButton );
btnSpeak.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
promptSpeechInput();
}
} );
}
private void promptSpeechInput() {
Intent intent = new Intent( RecognizerIntent.ACTION_RECOGNIZE_SPEECH );
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE_MODEL,
RecognizerIntent.LANGUAGE_MODEL_FREE_FORM );
intent.putExtra( RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault() );
intent.putExtra( RecognizerIntent.EXTRA_PROMPT,
getString( R.string.speech_prompt ) );
intent.putExtra( RecognizerIntent.EXTRA_SPEECH_INPUT_MINIMUM_LENGTH_MILLIS, 20000000 );
try {
startActivityForResult( intent, REQ_CODE_SPEECH_INPUT );
} catch (ActivityNotFoundException a) {
Toast.makeText( getApplicationContext(),
getString( R.string.speech_not_supported ),
Toast.LENGTH_SHORT ).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult( requestCode, resultCode, data );
switch (requestCode) {
case REQ_CODE_SPEECH_INPUT: {
if (resultCode == RESULT_OK && null != data) {
final ArrayList<String> result = data
.getStringArrayListExtra( RecognizerIntent.EXTRA_RESULTS );
//this is the real problem.
txtSpeechInput.setText( previous + " " + result.get( 0 ) );
previous = txtSpeechInput.getText().toString();
txtSpeechInput.setText( previous );
}
break;
}
}
}
}
I am a beginner in android and I am trying to learn how to use intent.
In my code, I am trying to send 2 integers to a different activity and perform some calculation and return back in the Main activity with the answer.
This is my main activity and with a click of a button it should send 10, 50 to my calculate class and from there I will click operator buttons like add, multiply, divide and send back the answer here in main activity.
So far I am able to received these numbers to my Calculate class and perform operation there but I am not sure how to get back here in main activity with the answers.
Main Activity
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getIntent() != null){
String msg = getIntent().getStringExtra("MSG");
int result = getIntent().getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivity(i);
}
}
Calculate class
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent i = new Intent(this, MainActivity.class);
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
}
}
you can use startActivityForResult and onActivityResult methods. fist in main activity you call startActivityForResult that means you want to get result from secondActivity (Calculate activity) and then override onActivityResult in main activity to get result.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(getIntent() != null){
String msg = getIntent().getStringExtra("MSG");
int result = getIntent().getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivityForResult(i, 1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String result=data.getStringExtra("result");
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult
}
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent returnIntent = Intent(this, MainActivity.class);
returnIntent.putExtra("result",calc );
setResult(RESULT_OK,returnIntent);
finish();
}
}
public class Calculate extends Activity{
private int calc;
private int first;
private int second;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent i = getIntent();
if(getIntent() != null){
String msg = i.getStringExtra("MSG");
first = i.getIntExtra("first", 0);
second = i.getIntExtra("second", 0);
Toast.makeText(this, "" + msg + first + " and " + second, Toast.LENGTH_LONG).show();
}
}
public void add(View v){
calc = first + second;
Intent i = new Intent(this, MainActivity.class);
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
startActivity(i);
}
}
You need a startActivity(i); in add() function.
You can do this by
onActivityResult()
method of android intent.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(data.getExtras().containsKey("widthInfo")){
width.setText(data.getStringExtra("widthInfo"));
}
if(data.getExtras().containsKey("heightInfo")){
height.setText(data.getStringExtra("heightInfo"));
}
}
See these links for more info:
http://developer.android.com/training/basics/intents/result.html
http://www.java2s.com/Code/Android/UI/CheckActivityresultandonActivityResult.htm
http://www.mybringback.com/android-sdk/12204/onactivityresult-android-tutorial/
It looks like you should be using startActivityForResult() here.
So, in your MainActivity, your onClick() would be modified to call startActivityForResult():
public void onClick(View v){
Intent i = new Intent(this, Calculate.class);
i.putExtra("MSG", "Receive two numbers : ");
i.putExtra("second", 10);
i.putExtra("first", 50);
startActivityForResult(i, 100); //modified
}
Then in MainActivity, you would also add onActivityResult to get the result:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK){
String msg = data.getStringExtra("MSG");
int result = data.getIntExtra("result", 0);
Toast.makeText(this, "" + msg + result, Toast.LENGTH_LONG).show();
}
}
Then, in the Calculate Activity, you would calculate the result, and send it back to MainActivity in an Intent, which will send the the MSG and result to onActivityResult() in MainActivity:
public void add(View v){
calc = first + second;
Intent i = new Intent(); //modified
i.putExtra("MSG", "Answer");
i.putExtra("result",calc);
setResult(RESULT_OK,i); //added
finish(); //added
}
Note that the reason it would be better for you to use startActivityForResult() here is that it would prevent having to add multiple Activities onto the back stack.
Say for example, you do two calculations (two trips from MainActivity to Calculate and back).
If you just used startActivity(), your back stack would then look like this:
MainActivity->Calculate->MainActivity->Calculate->MainActivity
And, it would just keep growing the more calculations you do.
Using startActivityForResult(), you are always just adding an instance of Calculate to the back stack, and then popping it and returning to the same instance of MainActivity. So, your back stack would always be just:
MainActivity
or...
MainActivity->Calculate
depending on which Activity you are currently in. As you can see, this is a huge improvement over the first option, which just keeps growing as you do more calculations.
I try to pass a string from one activity to another activity.
This is the coding in Activity A:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("UserName", UserName);
Log.i(Tag, "UserName1: "+ UserName);
super.onSaveInstanceState(savedInstanceState);
}
In Activity B I use this code to get the string:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
setUpViews();
if (savedInstanceState != null){
UserName = savedInstanceState.getString("UserName");
}
Log.i(Tag, "UserName2: "+ UserName);
}
But the logcat shown the first log "UserName1" when I clikc the open to Activity B,
and show the second log "UserName2" as "null".
May I know what wrong with my code?
What I want to do is Activity A pass the String in Activity B when I click the "button" and intent to Activity B. So I can get the String value in Activity B.
Any Idea? Cause I getting error when using intent.putStringExtra() and getintent.getStringExtra(), so I change to use onSaveInstanceState (), but still having problem.
EDIT:
This is my original code, I can get the String in Activity B, but unexpected I can't save my data in Sqlite. If remove the putString Extra then everything go smoothly.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent addItem = new Intent(ItemActivity.this, AddEditItem.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
addItem.putStringExtra("UserName", UserName);
Log.e(Tag, "UseName: "+ UserName);
startActivity(addItem);
return super.onOptionsItemSelected(item);
}
Code in Activity B:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
setUpViews();
UserName = (String) getIntent().getStringExtra("UserName");
Log.e(Tag, "UserName3: "+ UserName);
}
Full code for Activity B:
public class AddEditItem extends Activity implements OnItemSelectedListener {
private static final String Tag = null;
private EditText inputItemName;
private EditText inputItemCondition;
private EditText inputEmail;
private Button btnGal, btnConfirm;
private Bitmap bmp;
private ImageView ivGalImg;
private Spinner spinner;
String[] category = {"Books", "Clothes & Shoes", "Computer", "Electronics", "Entertainment", "Food & Drinks",
"Furnitures", "Mobile Phone", "Other", "UKM"};
String selection;
String filePath, itemName, itemCondition;
String UserName, user;
private int id;
private byte[] blob=null;
byte[] byteImage2 = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_item);
setUpViews();
if (savedInstanceState != null){
UserName = savedInstanceState.getString("UserName");
}
Log.e(Tag, "UserName2: "+ UserName);
//UserName = (String) getIntent().getStringExtra("UserName");
//Log.e(Tag, "UserName3: "+ UserName);
}
private void setUpViews() {
inputItemName = (EditText)findViewById(R.id.etItemName);
inputItemCondition = (EditText)findViewById(R.id.etItemCondition);
inputEmail = (EditText)findViewById(R.id.etEmail);
ivGalImg = (ImageView) findViewById(R.id.ivImage);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddEditItem.this, android.R.layout.simple_spinner_dropdown_item, category);
spinner = (Spinner)findViewById(R.id.spnCategory);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
Bundle extras = getIntent().getExtras();
if (extras != null) {
id=extras.getInt("id");
user=extras.getString("user");
inputItemName.setText(extras.getString("name"));
inputItemCondition.setText(extras.getString("condition"));
inputEmail.setText(extras.getString("email"));
selection = extras.getString("category");
byteImage2 = extras.getByteArray("blob");
if (byteImage2 != null) {
if (byteImage2.length > 3) {
ivGalImg.setImageBitmap(BitmapFactory.decodeByteArray(byteImage2,0,byteImage2.length));
}
}
}
btnGal = (Button) findViewById(R.id.bGallary);
btnGal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 0);
}
});
btnConfirm = (Button) findViewById(R.id.bConfirm);
btnConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (inputItemName.getText().length() != 0 && inputItemCondition.getText().length() != 0
&& inputEmail.getText().length() != 0) {
AsyncTask<Object, Object, Object> saveItemTask = new AsyncTask<Object, Object, Object>() {
#Override
protected Object doInBackground(Object... params) {
saveItem();
return null;
}
#Override
protected void onPostExecute(Object result) {
Toast.makeText(getApplicationContext(),
"Item saved", Toast.LENGTH_LONG)
.show();
finish();
}
};
saveItemTask.execute((Object[]) null);
Toast.makeText(getApplicationContext(),
"Item saved reconfirm", Toast.LENGTH_LONG)
.show();
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(
AddEditItem.this);
alert.setTitle("Error In Save Item");
alert.setMessage("You need to fill in all the item details");
alert.setPositiveButton("OK", null);
alert.show();
}
}
});
}
private void saveItem() {
if(bmp!=null){
ByteArrayOutputStream outStr = new ByteArrayOutputStream();
bmp.compress(CompressFormat.JPEG, 100, outStr);
blob = outStr.toByteArray();
}
else{blob=byteImage2;}
ItemSQLiteConnector sqlCon = new ItemSQLiteConnector(this);
if (getIntent().getExtras() == null) {
sqlCon.insertItem(UserName, inputItemName.getText().toString(),
inputItemCondition.getText().toString(),
inputEmail.getText().toString(),
selection, blob);
}
else {
sqlCon.updateItem(id, UserName, inputItemName.getText().toString(),
inputItemCondition.getText().toString(),
inputEmail.getText().toString(),
selection, blob);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent resultdata) {
super.onActivityResult(requestCode, resultCode, resultdata);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
Uri selectedImage = resultdata.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
// Convert file path into bitmap image using below line.
bmp = BitmapFactory.decodeFile(filePath);
ivGalImg.setImageBitmap(bmp);
}
}
}
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// TODO Auto-generated method stub
TextView tv = (TextView)view;
selection = tv.getText().toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
onSaveInstanceState is not used in that purpose, its used to save your activity state on for example orientation change, or when you leave an activity and get back to it.
What you need is to use intents.
Other than starting activity, intents can also carry some information throughout app, like this:
This would be activity 1:
Intent = new Intent (getApplicationContext(), Activity2.class);
intent.putExtra("UserName", UserName);
startActivity(intent);
and to recover it in second activity use:
String username = getIntent().getExtras().getString("UserName");
May I know what wrong with my code?
onSaveInstanceState() has nothing to do with passing data between activities. Instead, you need to put your string in an extra on the Intent used with startActivity().
Cause I getting error when using intent.putExtra() and getintent.getExtra()
Since that is the correct approach (albeit using getStringExtra()), please go back to it and fix whatever error you are encountering.