-1

I must be doing something very silly, but even after a whole day, I cant call onActivityResult on MainActivity from another activity.

In my AuxActivity, I have

 adapter.setOnItemClickListener(new CityListAdapter.ClickListener() {
      @Override
      public void onItemClick(View v, int position) {
        City city =adapter.getCityAtPosition(position);
//        Toast.makeText(getApplicationContext(),
//            city.getCity()+"\n"+city.getLatitude()+"\n"+city.getLongitude(),
//            Toast.LENGTH_LONG).show();
        Double Lat = city.getLatitude();
        Double Long = city.getLongitude();
        Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);
        mainIntent.putExtra("Lat", Lat);
        mainIntent.putExtra("Long", Long);
        mainIntent.putExtra("DbResultCode", Db_ACTIVITY_REQUEST_CODE);
        startActivityForResult(mainIntent, Db_ACTIVITY_REQUEST_CODE);
        finish();
      }
    });

In MainActivity, I am doing

//  Inside onCreate
//    Check Intent from Db
    Intent intent = getIntent();
    if (intent != null) {
      Lat = intent.getDoubleExtra("Lat", 0.0);
      Long = intent.getDoubleExtra("Long", 0.0);
      DbResultStat = intent.getIntExtra("DbResultCode", 0);
    }

I am getting values of Lat and Long properly here. But, then,

@Override
  protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(this, ""+requestCode, Toast.LENGTH_LONG).show();
    if (requestCode == DbActivity.Db_ACTIVITY_REQUEST_CODE){
     if (resultCode==RESULT_OK){
       setupViewPager();
     }
   }

This part is never called. I am still learning android, using the codelabs. So, anything new is giving me a lot of headache.

What I am doing wrong here?

BaRud
  • 3,055
  • 7
  • 41
  • 89
  • I believe you may be using `startActivityForResult` incorrectly. If you want to use `AuxActivity` to get a result for your main activity, then from `MainActivity` you would call `startActivityForResult` to start your `AuxActivity`. Then when ready use `setResult` in `AuxActivity`, and finish. Maybe [this](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) question will help you. – Quinn Mar 02 '20 at 17:22

1 Answers1

0

Try these two steps:-

1)Pass Context of Main Activity instead of Application:

Intent mainIntent = new Intent(getApplicationContext(), MainActivity.class);

to:

Intent mainIntent = new Intent(this, MainActivity.class);

Then Run

2)Remove Super from override method of onActivityResult: super.onActivityResult(requestCode, resultCode, data);

Then Run

Hope it Helps

Nikos Hidalgo
  • 3,666
  • 9
  • 25
  • 39