Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, August 6, 2016

How do I transfer data from one activity to another in Android?

How do I transfer data from one activity to another in Android?


I want to transfer my "result" data from my first (Main) acitivity to my Custaddress activity, which has edit texts for customer details, and then this is sent to an email. The email/edit texts work perfectly - but I want to add in "result.toString" into email body string. How do I transfer "result" to the second activity? I believe its something to do with arg? Here's my code from first activity..

DecimalFormat decimalFormat = new DecimalFormat(COMMA_SEPERATED);            result.append("\nTotal: ?"+decimalFormat.format(totalamount));       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);        alertDialogBuilder.setMessage(result.toString());        alertDialogBuilder.setTitle("YOUR ORDER");        alertDialogBuilder.setPositiveButton("Accept",         new DialogInterface.OnClickListener() {             @Override           public void onClick(DialogInterface arg0, int arg1) {              //do what you want to do if user clicks ok               //Intent intent = new Intent(context, Custaddress.class);              // startActivity(intent);               Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);               startActivity(custaddress);           }        });        alertDialogBuilder.setNegativeButton("Decline",         new DialogInterface.OnClickListener() {             @Override           public void onClick(DialogInterface dialog, int which) {              //do what you want to do if user clicks cancel.           }        });          AlertDialog alertDialog = alertDialogBuilder.create();        alertDialog.show();  

Answer by Maxouille for How do I transfer data from one activity to another in Android?


You should use intent (click here) :

Intent intent = new Intent(getBaseContext(), CustAdrresActivity.class);  intent.putExtra("text", mytext);  startActivity(intent);  

Answer by Gabriella Angelova for How do I transfer data from one activity to another in Android?


You just need to replace your lines:

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);  startActivity(custaddress);  

with these three lines:

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);  custaddress.putExtra("result", result.toString());  startActivity(custaddress);  

and then, when you open the new activity (in your case the Custaddress Activity), you should do the following to get your result

Bundle extras = getIntent().getExtras();  if (extras != null) {      String value = extras.getString("result");  }      

Answer by Samiullah Farooqui for How do I transfer data from one activity to another in Android?


You should add Extra in the Intent object which you are passing in the startActivity(intent) method. Example

String value = "String i want to send to next activity"  Intent intent = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);  intent.putExtra("KEY", value);  startActivity(intent);  

In the Custaddress.java Activity class you need to get the data from the Bundle object that you get as a parameter in the onCreate(Bundle bundle) method

Bundle extras = getIntent().getExtras();  if (extras != null) {      // get data via the key      String valueFromPreviousActivity = extras.getString("KEY");        if(valueFromPreviousActivity != null){           // do something with the data      }  }  

Answer by Mitesh Jobanputra for How do I transfer data from one activity to another in Android?


Write it in activity that passing data

Intent custaddress = new Intent(getApplicationContext(),com.example.frytest.Custaddress.class);  custaddress.putExtra("key",value);  startActivity(custaddress);  

Write below code in activity that catching data

Intent intent=getIntent();  String mString=intent.getStringExtra("key");  

hope this will help you

Answer by wwang for How do I transfer data from one activity to another in Android?


Check out this official doc Starting Another Activity.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.