return back data to another activity - android

i have two activities A and B i am using a button to be directed to activity B i managed to write a code so it code be turned back to activity A automatically without any buttons after 5 seconds
but now i want when it return back to return data to A how it can be done
i wrote the code in A to recieve data but don't know what to add in activity B
Activity A ::
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button two = (Button) findViewById(R.id.button2);
Recieve = (TextView) findViewById(R.id.textView4);
two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Intent Intent = new Intent(MainActivity.this, Gps.class);
//startActivity( Intent);
sendMessage();
}
});
}
public void sendMessage() {
Intent intent = new Intent(MainActivity.this, Hello.class);
startActivityForResult(intent, REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
Response = data.getStringExtra("key");
Recieve.setText("msg is " + Response);
}
}
Activity B::
public class Hello extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
final Intent i = new Intent(Hello.this, MainActivity.class);
Hello.this.startActivity(i);
Hello.this.finish();
}
}, 5000);
}
}

Related

How to send data by click back button?

I'm using two activities, MainActivity & SubActivity.
MainActivity.class
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivity(sub);
}
}
}
}
SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
In this case, if I change the text in EditText in SubActivity, I want to send changed text data to MainActivity when back button pressed in SubActivity(It means SubActivity onDestroy()). I don't want to make the second MainActivity like this in SubActivity.
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
startActivity(intent);
}
Is there any way to send text data when SubActivity removed from the activity stack?
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", "a");
startActivityForResult(sub , 2);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed here it is 2
if(requestCode==2)
{
String message=data.getStringExtra("name");
textView1.setText(message);
}
}
}
And
#Override
public void onBackPressed() {
Intent intent = new Intent (this, MainActivity.class);
intent.putExtra("name", text.getText().toString());
setResult(2,intent);
}
You need to use startActivityForResult() if you expect to have a return from the started activity.
After that, you'll also need to implement onActivityResult() method on the start activity to actually read the returned data.
Use startActivityForResult instead of startActivity
MainActivity :
public class MainActivity extends AppCompatActivity {
Button button;
String name= "a";
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sub = new Intent(this, SubActivity.class);
sub.putExtra("name", name);
startActivityForResult(sub,100);
}
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 100) {
String name = data.getStringExtra("name);
text.setText(name);
}
}
}
}
SubActivity:
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(getIntent().getStringExtra("name"));
}
#Override
public void onBackPressed() {
Intent resultIntent = new Intent();
resultIntent.putExtra("name", "your_editext_value");
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can use SharedPreference for storing value and update it or use it anywhere instead of using Intent
like In MainActivity
public class MainActivity extends AppCompatActivity {
Button button;
#Override
protected void onCreate() {
...
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Editor edit=sp.edit();
edit.putString("name","a");
edit.commit();
Intent sub = new Intent(this, SubActivity.class);
startActivity(sub);
}
}
} }
and In SubActivity
public class SubActivity extends AppCompatActivity {
EditText text;
#Override
protected void onCreate() {
...
text.setText(sp.getString("name",""));
text.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Editor edit=sp.edit();
edit.putString("name",s.toString);
edit.commit();
} });
}
#Override
public void onBackPressed() {
super.onBackPressed();
}}
To use value from SharedPreference
sp.getString("name","");
For using SharedPreference look at this https://developer.android.com/training/data-storage/shared-preferences#java

How to take the QR scanner result and make it into a string?

How do I take the 'this' value ( from the 6th line) and make it into a string?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qrscanner);
fragment = (BarcodeFragment)getSupportFragmentManager().findFragmentById(R.id.sample);
fragment.setScanResultHandler(this);
btn = ((Button)findViewById(R.id.scan));
btn.setEnabled(false);
}
For example instead of just toasting the value, can I use the value and make it into a string and then carrying it into another activity.
#Override
public void scanResult(ScanResult result) {
btn.setEnabled(true);
Intent intent = new Intent(QrscannerActivity.this,ProductInfoActivity.class);
startActivity(intent);
Toast.makeText(this, result.getRawResult().getText(), Toast.LENGTH_LONG).show();
}
You should use the Google ZXing Barcode Scanner library and with the below code:
extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
HandleClick hc = new HandleClick();
findViewById(R.id.butQR).setOnClickListener(hc);
findViewById(R.id.butProd).setOnClickListener(hc);
findViewById(R.id.butOther).setOnClickListener(hc);
}
private class HandleClick implements OnClickListener{
public void onClick(View arg0) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
switch(arg0.getId()){
case R.id.butQR:
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
break;
case R.id.butProd:
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
break;
case R.id.butOther:
intent.putExtra("SCAN_FORMATS", "CODE_39,CODE_93,CODE_128,DATA_MATRIX,ITF,CODABAR");
break;
}
startActivityForResult(intent, 0); //Barcode Scanner to scan for us
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
TextView tvStatus=(TextView)findViewById(R.id.tvStatus);
TextView tvResult=(TextView)findViewById(R.id.tvResult);
if (resultCode == RESULT_OK) {
tvStatus.setText(intent.getStringExtra("SCAN_RESULT_FORMAT"));
tvResult.setText(intent.getStringExtra("SCAN_RESULT"));
} else if (resultCode == RESULT_CANCELED) {
tvStatus.setText("Press a button to start a scan.");
tvResult.setText("Scan cancelled.");
}
}
}
}

How to implement startActivityForResult

In my app I create two activities and I want to get input from the second activity and use it in the first activity, I use startActivityForResult but there is a problem in it!
That is the code of first activity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final Button b=(Button)findViewById(R.id.button);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent("net.naif.action.GETDATA");
startActivityForResult(i,77);
}
});
}
protected void onActivityForResult (int requestCode,int resultCode,Intent data){
if (requestCode ==77 && resultCode ==RESULT_OK){
String msg =data.getStringExtra("text");
Toast.makeText(getBaseContext(),msg,Toast.LENGTH_SHORT).show();
}
}
And this for the second:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
final Button b=(Button)findViewById(R.id.button);
final EditText t=(EditText)findViewById(R.id.editText);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String s=t.getText().toString();
Intent i=new Intent();
i.putExtra("text",s);
setResult(Activity.RESULT_OK,i);
finish();
}
});
}
Android studio notify me that is the method ( protected void onActivityForResult (int requestCode, int resultCode, Intent data) is never used.
That's because the method is named onActivityResult, not onActivityForResult.

Cant getting result from child activity

I have a Parent activity which is sending data to child activity but the child is not returning the result. I posted portion of code from both activity altogether below- Theres a few code that I didnt post for unnecessity.
public class TdeeActivity extends Activity {
public static final int CALLED_ACTIVITY = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tdee);
Button ok=(Button)findViewById(R.id.btnOk);
ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent bmr= new Intent(TdeeActivity.this,BMRActivity.class);
startActivityForResult(bmr,CALLED_ACTIVITY);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
Toast.LENGTH_SHORT).show();
}
}
}
} // end class
public class BMRActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bmr);
Button btnOk=(Button)findViewById(R.id.btnOk);
btnOk.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showResult();
}
});
public void showResult() {
Intent data= new Intent();
data.putExtra("result",result);
setResult(RESULT_OK, data);
finish();
}
}
}// end class
I tested you code in a dummy project and it was working fine at my end.. following is the code for both activity:
public class ParentActivity extends Activity {
private static final int CALLED_ACTIVITY = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent bmr = new Intent(ParentActivity.this, ChildActivity.class);
startActivityForResult(bmr, CALLED_ACTIVITY);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case CALLED_ACTIVITY:
if (resultCode == RESULT_OK) {
Toast.makeText(this, "THE RESULT-"+data.getExtras().getString("result"),
Toast.LENGTH_SHORT).show();
}
}
}
}
public class ChildActivity extends Activity {
final Activity activity = this;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Returning result from child activity
Intent data = new Intent();
data.putExtra("result", "from child"
+ this.getCallingActivity().getClassName());
setResult(RESULT_OK, data);
finish();
}
}
So please post more code so that issue can be found..

can't call onactivity result function after child activity is finished

i am using following codes.
addNewReceipt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), AddReceipt.class);
myIntent.putExtra("receiptList", receipts);
startActivityForResult(myIntent, RECEIPT_ADD);
}
});
}
#SuppressWarnings("unchecked")
public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == RECEIPT_ADD)
{
receipts = (ArrayList<Receipt>) data.getSerializableExtra("receiptList");
addReceiptsInListView();
}
}
}
The code for AddReceipt class is as follow,
#Override
public void finish() {
Intent data = new Intent();
data.putExtra("receiptList", receipts);
setResult(RESULT_OK, data);
super.finish();
}
#SuppressWarnings("unchecked")
public void onCreateCall()
{
done.setOnClickListener(new OnClickListener()
{ //receiptAddBtn
#Override
public void onClick(View v)
{
String error = "";
Receipt receipt = new Receipt();
receipt.comments = comments.getText().toString();
receipt.referenceNo = receiptNo.getText().toString();
receipt.image = imageSelected;
if (receipt.image == null || receipt.referenceNo == "" )
{
error = "Please input Receipt No. and attach Image";
displayAlert(error);
}
else
{
receipts.add(receiptCounter,receipt);
finish();
}
}
});
}
The problem is, when the activity is finished... and i pass this receipt, in my previous class from which i call this activity public synchronized void onActivityResult Never works.
The back button is not ending activity either.
Please tell me where i am wrong,.
Best Regards
Here is the solution to your problem
#Override
public void finish() {
Intent data = new Intent();
data.putExtra("receiptList", receipts);
setResult(Activity.RESULT_OK, data);
super.finish();
}

Categories

Resources