About Intent requestCode, Android - android

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));
}
}
}
}

Related

Use onActivityResult when button IsClicked

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
}
};

How to pass variable to onActivityResult() from populateViewHolder?

I have a Firebase Recycler View which contains a button like this
final String post_key = getRef(position).getKey();
viewHolder.btnUpload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent data = new Intent();
data.putExtra("post_key",post_key);
Log.d("post_key", data.getExtras().getString("post_key"));
data.setType("image/*");
data.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(data, GALLERY_REQUEST);
}
});
This will trigger an action in OnActivityResult(). I was trying to access the variable "post_key" in OnActivityResult. Therefore, I put it to the extra, and call the getString function in OnActivityResult.
final String post_key=data.getExtras().getString("post_key");
However, the app crashed and keeps telling me that I attempt to invoke virtual method 'getString' on a null object reference
You need to check the request code and the result code as well:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
// here you can access the intent
String msg = (data != null) ? data.getStringExtra("post_key") : "";
Log.d("post_key", "Got post_key: " + msg);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
You can not send your data to another app - if it does not accept. The thing you are doing is not mentioned anywhere.
Correct Way
in onClick don't pass extra, and hold your post_key in your activity/ adapter.
adapter.setPostKey(post_key);
Then in onActivityResult() get this post_key.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GALLERY_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
String post_key = adapter.getPostKey();
}
}
super.onActivityResult(requestCode, resultCode, data);
}

How can i pass onActivityResult data in activity?

#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 );

onActivityResult error when I press "cancel" button on my second Activity

I have the next activities:
Activity1
//declare
private static final int SAVE_DATA_FROM_ACTIVITY = 203;
//........... not important code
//button to open second Activity
public void btn_openSecondActivity(View view)
{
Intent intent = new Intent(Activity2.this, Activity1.class);
startActivityForResult(intent, SAVE_DATA_FROM_ACTIVITY);
}
}
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
}
//....... not important code
Activity2
On Second Activity I have two buttons:
Cancel
Save
//............
//declare
private static final int OK_RESULT_CODE = 1;
//Cancel button
public void btn_cancel(View view)
{
finish();
}
//Save button
public void btn_save (View view)
{
Intent intent = new Intent();
intent.putExtra("Name",et_name.getText().toString());
setResult(OK_RESULT_CODE, intent);
finish();
}
PROBLEM
When I click Save button all works perfect, but the problem it's when I click Cancel button, then it's reports an error:
Failure delivering result ResultInfo{who=null, request=203, result=0, data=null} to activity {com.example.alvaro.project/com.alvaro.project.Activity1}: java.lang.NullPointerException
I understand the problem, when I cancel is not the same result code but i don't know how I can solve it
Any suggestions?
You have issue in onActivityResult method. You don't check result.
Change your condition from :
if (requestCode == SAVE_DATA_FROM_activity)
to:
if (resultCode == OK_RESULT_CODE && requestCode == SAVE_DATA_FROM_activity)
Change this
if (requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
into
if (requestCode == SAVE_DATA_FROM_activity&&resultCode==RESULT_OK)
{
name= data.getStringExtra("Name");
}
and
Your cancel method is like
public void btn_cancel(View view)
{
setResult(RESULT_CANCELED);
finish();
}
And instead of OK_RESULT_CODE use Android default ok like Activity.RESULT_OK
Check if you manage to set the result
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (requestCode == SAVE_DATA_FROM_activity && resultCode = Activity2.OK_RESULT_CODE)
{
name= data.getStringExtra("Name");
} else {
//probably btn_cancel pressed
}
}
check resultCode in onActivityResult
protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
if (resultCode == 1/*OK_RESULT_CODE from Second Activity */ && requestCode == SAVE_DATA_FROM_activity)
{
name= data.getStringExtra("Name");
}
}
Activity1
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SAVE_DATA_FROM_activity) {
if (resultCode == Activity.RESULT_OK) {
name = data.getStringExtra("Name");
} else if (resultCode == Activity.RESULT_CANCELED){
// TODO something
}
}
}
Activity2
delete field OK_RESULT_CODE
//Cancel button
public void btn_cancel(View view) {
setResult(Activity.RESULT_CANCELED, new Intent());
finish();
}
//Save button
public void btn_save(View view) {
Intent intent = new Intent();
intent.putExtra("Name", et_name.getText().toString());
setResult(Activity.RESULT_OK, intent);
finish();
}

Wait for activity to finish in non-activity class

I have a method in my application which starts a new Activity which starts the Camera Application. The Camera Application returns a result (picture taken or not taken), and I store this result in the Intent-Bundle.
In the original method I want to read this bundle, which does not work because the activity is not finished yet.
How can I wait for the activity to finish before going on in my method?
Method
#Override
public boolean startChallenge(Context context) {
Intent cam = new Intent(context, CameraIntent.class);
cam.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(cam);
Boolean done = cam.getExtras().get("done"); // << this fails obviously
}
Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_CAPTURE_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
finish();
}
I would like to keep the structure of the code because the startChallenge() Method is inherited from a superclass (Challenge) and startChallenge is different from type to type (e.g. there is a challenge where you have to take a picture, another challenge where you have to answer something, and so on). The method gets called in another activity, depending on the type of challenge.
startChallenge should have a reference to the Activity that expects the result back to be able to call startActivityForResult
Method
#Override
public boolean startChallenge(Activity activityB) {// Activity B in which you are expecting this result back.
Intent cam = new Intent(activityB, ActivityA.class);
activityB.startActivityForResult(cam, REQUEST_CODE_OPEN_CAMERA);
}
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
intent = getIntent();
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
intent.putExtra("done", true);
} else if (resultCode == RESULT_CANCELED) {
intent.putExtra("done", false);
}
}
setResult(RESULT_OK, intent);
finish();
}
Another approach would be to use LocalBroadcastManager or Otto
Activity A
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_CAPTURE_CODE) {
if (resultCode == RESULT_OK) {
BusProvider.getInstance().post(new ImageCapturedEvent(true));
} else if (resultCode == RESULT_CANCELED) {
BusProvider.getInstance().post(new ImageCapturedEvent(false));
}
}
finish();
}

Categories

Resources