Search code examples
javascriptandroidsqliteandroid-studioback-button

Passing The Proper Information To Previous Activity When Back Button Pressed


I am using the following code to implement the back button in the toolbar:

super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_series);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar( toolbar );
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(SeriesActivity.this, BowlerActivity.class));
                finish();
            }
        });

My problem is when the .setNavigationOnClickListener registers the click it goes back to the previous activity minus the proper Bowlers. When I go to the Series Activity I am passing the leagueId and the bowlerId to it. When I go back to the BowlerActivity I am not passing anything back so I get the following:

enter image description hereenter image description hereenter image description here

Bowlers B1 and B2 belong to a different League.

I have gone through several different threads like this one how to override action bar back button in android? and I have tried several of the different suggestions in them. None of them worked for me.

How do I pass the leagueId and the bowlerId back to the BowlerActivity so that when it starts up I am filtering the proper Bowler from the database to display in the listview.

Do I even need to pass these values back? Any assistance would be appreciated.


Solution

  • From your SeriesActivity call the BowlerActivity using startActivityForResult() method

    For example:

    Intent i = new Intent(SeriesActivity.this, BowlerActivity.class);
    startActivityForResult(i, 1);
    

    In your BowlerActivity set the data which you want to return back to SeriesActivity. If you don't want to return back, don't set any.

    For example: In BowlerActivity if you want to send back data:
    
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result",result);
    setResult(Activity.RESULT_OK,returnIntent);
    finish();
    
    If you don't want to return data:
    
    Intent returnIntent = new Intent();
    setResult(Activity.RESULT_CANCELED, returnIntent);
    finish();
    

    Now in your SeriesActivity class write following code for the onActivityResult() method.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    
        if (requestCode == 1) {
            if(resultCode == Activity.RESULT_OK){
                String result=data.getStringExtra("result");
            }
            if (resultCode == Activity.RESULT_CANCELED) {
                //Write your code if there's no result
            }
        }
    }
    

    Source => https://stackoverflow.com/a/10407371/9956766