Search code examples
androidandroid-intentandroid-activity

onActivityResult() not working on Android


I'm trying to use onActivityResult to send a title, I've looked at the google implementation for the same task but it doesn't work me. Can someone help?

code for onActivityResult in mainActivity.

public void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        System.out.println("There is something coming to this function" + requestCode);
        if(requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
            Title title = new Title(data.getStringExtra(notesSection.EXTRA_REPLY));
            mTitleViewModel.insert(title);
        }else{
            Toast.makeText(getApplicationContext(), R.string.empty_not_saved, Toast.LENGTH_LONG).show();
        }
    }

code in my notesSection activity.

finishFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // System.out.println("Button Has Been Clicked From Notes Section");
                Intent replyIntent = new Intent();
                if (TextUtils.isEmpty(titleEdit.getText())){
                    // System.out.println("Empty?");
                    setResult(RESULT_CANCELED, replyIntent);
                }else{
                    // System.out.println("Sending Something Supposedly");
                    String title = titleEdit.getText().toString();
                    // System.out.println("Sending " + title);
                    replyIntent.putExtra(EXTRA_REPLY, title);
                    setResult(RESULT_OK, replyIntent);
                }
                finish();
//                startActivity(new Intent(notesSection.this, MainActivity.class));
            }
        });

FYI: When I print something in the onActivityResult function, nothing shows up on my run terminal, I don't know why this is, but I don't think the function is being reached for some reason. I will send more code if necessary. Thanks.


Solution

  • In your first activity try starting second activity like this

    static int NEW_TITLE_ACTIVITY_REQUEST_CODE = 1;
    
     @Override
    protected void onCreate(Bundle savedInstanceState) {
        ---
    
        ---
    
        ---
    
    // i assume you are starting activity on some button click
    // so add following line in you button on click event
    
    startActivityForResult(new Intent(MainActivity.this,notesSection.class),NEW_TITLE_ACTIVITY_REQUEST_CODE);
    }
    

    then override following method in your FIRST ACTIVITY

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK && requestCode == NEW_TITLE_ACTIVITY_REQUEST_CODE)
        {
             // do whatever you want   
        }
    }
    

    Also update your finish fab on click listener

    finishFab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent replyIntent = new Intent();
                if (TextUtils.isEmpty(titleEdit.getText())){
                    setResult(RESULT_CANCELED, replyIntent);
                }
                else{
                    replyIntent.putExtra("TITLE", titleEdit.getText().toString());
                    setResult(RESULT_OK, replyIntent);
                }
                finish();
            }
        });