I have an activity that once a ToggleButton is clicked, it starts another activity as follows:
Intent intent = new Intent(ChatActivity.this, BBActivity.class);
startActivityForResult(intent,1);
In that second activity it performs few calculations and then:
confirmedData.putExtra( "confirmedB", (Serializable) bSelected );
confirmedData.putExtra( "dateInMillis",DateInMillis );
setResult(ChatActivity.RESULT_OK,confirmedData);
Then, I use the following to get the results in my first activity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
ArrayList<DiscoverB> confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
Now, I had like to use confirmedB Once a button in my activity is clicked.
How can I pass the values from onActivityResult to my ClickListener?
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
HERE I NEED MY confirmedB
}
};
Thank you
In your first activity, declare a variable
ArrayList<DiscoverB> confirmedB;
In OnActivityResult, you can initialize confirmedB
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == ChatActivity.RESULT_OK){
confirmedB = (ArrayList<DiscoverB>)data.getSerializableExtra("confirmedB");
Long DateInMillis = data.getLongExtra( "dateInMillis", System.currentTimeMillis());
Log.d("ERROR", "error");
}
if (resultCode == ChatActivity.RESULT_CANCELED) {
}
}
}
You can use it now in your clickListener,
ChatAdapter.OnConfirmClickListener confirmListener = new ChatAdapter.OnConfirmClickListener(){
#Override
public void onClick(Button confirmB) {
???myarraylist = onActivityResult(requestCode, resultCode, data)???
here you can use confirmedB
}
};
Related
Issue:
I have a Listview in Mainactivity. Each row of listview has two buttons say SET and RUN.
Pressing SET will take you to SET activity and if the user clicks save button in SET Activity, I need to disable the SET button in the corresponding row position of the listview in mainactivity.
So Far Done:
For that I have a refresh function on a onclicklistener to requery the list with updated values. How to call that refresh function without keypress in the Mainactivity or is there any other way?
Activity MAIN :
viewHolder.ButtonSET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String title = v.getTag().toString();
if (title.equals("SET")) {
if (Integer.parseInt((String) viewHolder.TDNQTY.getText()) > 0) {
if(scanoverornot(pos)<=0) {
Intent s = new Intent(DN.this, SETActivity.class);
s.putExtra("position", pos);
s.putExtra("mode", "SET");
try{
startActivityForResult(s, saverequestcode);
// getContext().startActivity(s);
}
catch(Exception e){
Toast.makeText(getContext(),""+e,Toast.LENGTH_LONG).show();
}
}
}
}
}
});
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data == null)
return;
switch (requestCode) {
case saverequestcode:
if (resultCode == RESULT_OK) {
String SItem= data.getStringExtra("SItem");
int SPos= data.getIntExtra("SPos", 0);
saved = 700;
Toast.makeText(getApplicationContext(), ""+ SItem+ SPos, Toast.LENGTH_LONG).show();
//btnvalidate.performClick();
}
}
}
Activity SET :
Intent sav = new Intent();
sav.putExtra("SItem", String.valueOf(itemno));
sav.putExtra("SPos", String.valueOf(pos));
setResult(RESULT_OK, sav);
finish();
You can use startActivityForResult to nail this purpose:
startActivityForResult(new Intent(this, DestinationActivity.class), MY_RESULT);
And then in your MainActivity:
public int MY_RESULT = 10;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_RESULT) {
if (resultCode == Activity.RESULT_OK) {
//refresh the list according to your logic
}
}
}
Don't forget to call setResult(Activity.RESULT_OK); when user clicks save button.
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setResult(Activity.RESULT_OK);
}
});
Issue Lines:
Have to add super.onActivityResult(requestCode, resultCode, data);
Removed switch case and used if condition for requestCode check
Solution:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null)
return;
if (requestCode == saverequestcode) {
if (resultCode == Activity.RESULT_OK) {
String SItem = data.getStringExtra("SItem");
String SPos = data.getStringExtra("SPos");
Toast.makeText(getApplicationContext(), "Item :" + SItem + "Position :" + SPos, Toast.LENGTH_LONG).show();
}
if (resultCode == Activity.RESULT_CANCELED) {
//Any methods
}
}
else if (requestCode == importrequestcode){
}
}
Activity SET :
Intent sav = new Intent();
sav.putExtra("SItem", String.valueOf(itemno));
sav.putExtra("SPos", String.valueOf(pos));
setResult(Activity.RESULT_OK,sav);
finish();
Couldn't find a good answer for this. I have a class(no opened from MainActivity.
There I want to call startActivityForResult, to know when to do something on the UI.
How do I do it correctly? I passed the activity and context to the class.
On class:
private void init(){
Intent TestIntent = new Intent();
mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
On MainActivity:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == TEST){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Check this, You have to pass code with Intent of Required Class
private void init(){
Intent TestIntent = new Intent(this,SecondClass);
startActivityForResult(TestIntent,222);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == 222){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
Your TestIntent is empty. It doesn't have anything in it that tells Android what Activity to start. You need to create your Intent like this:
private void init(){
Intent TestIntent = new Intent(mActivity, ActivityToStart.class);
mActivity.startActivityForResult(TestIntent,MainActivity.TEST);
}
private void init(){
Intent intent = new Intent(this,another.class);
startActivityForResult(intent,requestCode);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent Data) {
super.onActivityResult(requestCode, resultCode, Data);
if (requestCode == reruestCode){
Toast.makeText(this, "TEST", Toast.LENGTH_SHORT).show();
}
}
private void getResponse(){
setResult(RESULT_OK,new Intent());
finish();
//Use this block of code in the second class.
}
code in fragment
private static final int BRAINTREE_REQUEST_CODE = 777;
BottomMenu fragment = this;
public void onBraintreeSubmit(String clienTtoken) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(getActivity()), BRAINTREE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == BRAINTREE_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
DropInResult result = data.getParcelableExtra(DropInResult.EXTRA_DROP_IN_RESULT);
Toast.makeText(getActivity(), result.getPaymentMethodNonce().getNonce() + "", Toast.LENGTH_SHORT).show();
// use the result to update your UI and send the payment method nonce to your server
sendPaymentNonceToServer(result.getPaymentMethodNonce().getNonce());
} else if (resultCode == Activity.RESULT_CANCELED) {
// the user canceled
} else {
// handle errors here, an exception may be available in
Exception error = (Exception) data.getSerializableExtra(DropInActivity.EXTRA_ERROR);
}
}
}
code in activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Problem:
I have implemented BrainTree payment gateway with onBraintreeSubmit() method I startActivityResult and catch it onActivityResult but it's not being called in the fragment.
onActivityResult only gets called if I cancel payment but never when payment success or exception thrown
First you have to call from activity
Add below code in your activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frameLayout);
fragment.onActivityResult(requestCode, resultCode, data);
}
If you call startActivityForResult() with activity instance, then onActivityResult will get in your activity not in fragment. This is the reason for your issue.
Modified the code as below. This will solve your issue.
public void onBraintreeSubmit(String clienTtoken, BottomMenu context) {
DropInRequest dropInRequest = new DropInRequest().clientToken(clienTtoken);
startActivityForResult(dropInRequest.getIntent(context.getContext()), BRAINTREE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 900) {
if (resultCode == getActivity().RESULT_OK) {
Bundle b=data.getExtras();
if(b!=null){
Playlist playlist = (Playlist) b.getSerializable("obj");
int playlistId = data.getIntExtra("PLAYLIST_ID", 0);
Log.d("---->Data ID", String.valueOf(playlistId));
}
}
}
How can I send that playlistId Value in onCreate() method?
ResultActivity:
intent.putExtra("yourKeyName", "hello");
setResult(900, intent);
Get the result:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== 900) {
if (resultCode == getActivity().RESULT_OK) {
String hello = data.getStringExtra("yourKeyName");
}
}
You dont have to create a new Bundle, just get extra content from the "Intent data".
Hope this helps.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode== 900) {
if (resultCode == getActivity().RESULT_OK) {
Bundle b=data.getExtras();
if(b!=null){
Playlist playlist = (Playlist) b.getSerializable("obj");
int playlistId = data.getIntExtra("PLAYLIST_ID", 0);
Log.d("---->Data ID", String.valueOf(playlistId));
}
}
}
use this in another activity to get the result back
Intent intent = new Intent();
intent.putExtra("key",value);
setResult(900,intent );
Why my toast message is showing? My CEVAP_SORGULA variable equals 322, but my intents request code is 332
private final static int CEVAP_SORGULA = 322;
public void degistirActivity(final View view){
startActivityForResult(new Intent(this,veriTopla.class),332);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == Activity.RESULT_OK && requestCode == CEVAP_SORGULA ){
Toast.makeText(this, "Cevabin: " + data.getExtras().getInt("Cevap"),Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
I see clearly a type error between the CEVAP_SORGULA and the value passed via startActivityForResult. To fix it provide the variable instead of the hardcoded WRONG value.
startActivityForResult(new Intent(this,veriTopla.class),CEVAP_SORGULA);
you can try
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 332);
DEFAULT METHOD
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
SEE THIS EXAMPLE FROM ANDROID DEVELOPER SITE, MAKE YOU HELP
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}
}