0

Hey guys I'm not a very advanced programmer so i'm scratching my head at this weird issue I'm getting. Trying to make Activity A call Activity B so that it can add some data to a SQL Database and exit back into Activity A. The issue is Button on Activity B to return to Activity A is broken and won't even output any Android Monitor Logs.

Activity A calls B using:

private void btnFunc (){
    Button btnAddSubject = (Button) findViewById(R.id.btn_new_subject);
    btnAddSubject.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent myIntent = new Intent(v.getContext(), AddSubject.class);
            startActivityForResult(myIntent,1);
        }
    });

}

This Is what Activity B's return Function looks like it is called in the onCreate function:

public void Continue(){
    //SQL Instances
    subject = new SQLSubject(this);
    mContinue.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    boolean isInserted = subject.insertData(add_subject.getText().toString(),
                            add_subject_group.getText().toString(),
                            add_teacher.getText().toString());
                    if (isInserted == true) {
                        Toast.makeText(AddSubject.this, "Saved", Toast.LENGTH_LONG).show();
                        returnIntent = new Intent(v.getContext(), MainActivity.class);
                        setResult(1);
                        startActivity(returnIntent);
                    }else {
                        Toast.makeText(AddSubject.this, "Error saving data", Toast.LENGTH_LONG).show();
                        returnIntent = new Intent(v.getContext(), MainActivity.class);
                        setResult(0);
                        startActivity(returnIntent);

                    }
                }
            }
    );
}

The onCreate also includes:

finishActivity(1);
Pankaj
  • 7,908
  • 6
  • 42
  • 65

2 Answers2

1

You are not returning an Intent, you are restarting the other activity (and doing nothing with the returnIntent).

You should set the intent in the result and finish the current activity.

For example

setResult(1, returnIntent);
finish();

Then the code for onActivityResult will be called in the activity that had startActivityForResult.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

Instead of

setResult(1); and setResult(0);

call it as:

setResult(Activity.RESULT_OK,returnIntent); //instead of setResult(1);

and

setResult(Activity.RESULT_CANCELED, returnIntent); //instead of setResult(0);
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • That's just a constant, it doesn't really matter if the proper check is in `onActivityResult` – OneCricketeer Jan 03 '16 at 14:08
  • if its really like this then how [this solution](http://stackoverflow.com/a/10407371/2715073) have more than 1000 votes. – Pankaj Jan 03 '16 at 14:19
  • Because it was clearly explained? `if(resultCode == Activity.RESULT_OK)` could just as easily be `if(resultCode == 0)`. It's **just a constant** as it stands , this is a suggestion, not a solution – OneCricketeer Jan 03 '16 at 15:22