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);
            }
        });
Prabh deep
  • 1,024
  • 11
  • 15
  • 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. – Taslim Oseni Feb 20 '19 at 08: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. – Pratik Butani Feb 20 '19 at 09:11
  • Dont know why question got so many ups? just google a bit yourself. https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android. You clearly don't understand how ActivityForResult works. So I suggest you start reading about it asap. – Vygintas B Feb 20 '19 at 09:13
  • @VygintasB Read carefully. Its different question than others. – Pratik Butani Feb 20 '19 at 09:15
  • @PratikButani The answer is same. In short you can't call onActivityResult yourself. It is called then activity you open returns result. – Vygintas B Feb 20 '19 at 09:18
  • Check the answer here: [How to trigger a function on a old Activity when returning from a newer activity?](https://stackoverflow.com/a/54058428/8034839) – shizhen Feb 20 '19 at 09:19
  • @VygintasB before understanding the question how you mention duplicate ? – Prabh deep Feb 20 '19 at 10:40
  • @Prabhdeep I understand everything clearly. Just follow up answer. I'm 110% sure It's solution. And I ask you question. Why ask question before doing research yourself? – Vygintas B Feb 20 '19 at 10:45
  • @VygintasB i have already used this link – Prabh deep Feb 20 '19 at 10:48
  • @VygintasB please remove duplicate word – Prabh deep Feb 20 '19 at 10:49
  • @VygintasB i clearly mention that how to call fragment onActivityResult only not call Activity method – Prabh deep Feb 20 '19 at 10:52
  • @Prabhdeep It does not matter if it's in activity or fragment. Execution is still same.... – Vygintas B Feb 20 '19 at 11:05
  • @VygintasB please check practically its not working same – Prabh deep Feb 20 '19 at 11:09
  • @Prabhdeep Sorry, but I'm done with you. I'm trying to help you, but you keep your position same. Please read about `startActivityForResult` first. Error in your logcat is useless, because you not even starting activity right. There is answers bellow which point it out. – Vygintas B Feb 20 '19 at 11:12
  • i m telling you answer but please remove duplicate if the answer same with your link you can give negative vote okk – Prabh deep Feb 20 '19 at 11:17
  • Intent intent = new Intent(getContext(), XYZActivity.class); intent.putExtra("name","prabh"); startActivityForResult(intent , 45); use like this – Rahul Arora Feb 20 '19 at 13:08
  • i have already used this but activty reopen its not solution and this logic for call activity method not for fragment – Prabh deep Feb 20 '19 at 13:16

1 Answers1

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();
Rahul Arora
  • 334
  • 1
  • 10