5

enter image description hereplease write the answer how to call onActivityResult of fragment on button click

enter image description here

 Button button=view.findViewById(R.id.bbbtttnnn); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(); intent.putExtra("name","charan"); startActivityForResult(intent,45); } }); 
17
  • Just a sidenote: Try as much as possible to always give your IDs meaningful/relevant names. I learnt the hard way when I had to go back to a codebase I wrote two years earlier. It also helps other people that might want to work on your code. Commented Feb 20, 2019 at 8:16
  • Can you tell me exactly what/why you want to do? May be we can help with other ways as your code doesn't seems that you required like this. Commented Feb 20, 2019 at 9:11
  • Dont know why question got so many ups? just google a bit yourself. stackoverflow.com/questions/10407159/…. You clearly don't understand how ActivityForResult works. So I suggest you start reading about it asap. Commented Feb 20, 2019 at 9:13
  • @VygintasB Read carefully. Its different question than others. Commented Feb 20, 2019 at 9:15
  • @PratikButani The answer is same. In short you can't call onActivityResult yourself. It is called then activity you open returns result. Commented Feb 20, 2019 at 9:18

1 Answer 1

0

Define constant

public static final int REQUEST_CODE = 1; 

Use intent

Intent intent = new Intent(Activity.this, XYZActivity.class); startActivityForResult(intent , REQUEST_CODE); 

Now use onActivityResult to retrieve the result

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { try { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) { String requiredValue = data.getStringExtra("key"); } } catch (Exception ex) { Toast.makeText(Activity.this, ex.toString(), Toast.LENGTH_SHORT).show(); } } 

In next screen use this code to set result

Intent intent = getIntent(); intent.putExtra("key", value); setResult(RESULT_OK, intent); finish(); 
Sign up to request clarification or add additional context in comments.

6 Comments

but i used fragment
Then what issue you are facing ?
appclication crashed on button click
Can you show error log
@RahulArora , The answer you have given will simply open the activity , which he does not want to ,
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.