1

There's a lot of questions about this already, and I feel like my code should work. What I'm trying to do is Activity A starts Activity B, sending an intent. Activity B then starts Activity A for result, and a new Intent should be received. My code is below, and according to the quote from the documentation below, the code should be working:

The "standard" and "singleTop" modes differ from each other in just one >respect: Every time there's new intent for a "standard" activity, a new >instance of the class is created to respond to that intent. Each instance >handles a single intent. Similarly, a new instance of a "singleTop" activity >may also be created to handle a new intent. However, if the target task >already has an existing instance of the activity at the top of its stack, >that instance will receive the new intent (in an onNewIntent() call); a new >instance is not created.

So based on that, my code is as follows: P.S My onNewIntent(Intent) doesn't get called, Im wondering if i put it in the right class.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drink_sale_list);

    mListView = (ListView) findViewById(R.id.list);
    mDrinksAdapter = new drinksAdapter(getApplicationContext(), drinks, drinkPics);
    mListView.setAdapter(mDrinksAdapter);

    mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    //If the bn!= null, this means that this activity was started by Activity B via startActivityForResult()

            Bundle bn = getIntent().getExtras();
            if(bn != null){

                    Intent intent = new Intent();
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("anotherDrinkObject", drinks.get(position));
                    intent.putExtras(bundle);
                    intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
                    setResult(RESULT_OK, intent);

                    finish();

            }
            else {

                Intent intent = new Intent(drinkSaleList.this, SaleActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("DrinkObject", drinks.get(position));
                intent.putExtras(bundle);

                startActivity(intent);
            }
        }

    });
}

}

Activity B

public class SaleActivity extends AppCompatActivity {

private ListView mListView;
private drinksAdapter mDrinksAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_sale);
    setContentView(R.layout.activity_sale);
    Bundle bundle = getIntent().getExtras();
    Drink drink = (Drink) bundle.getSerializable("DrinkObject");

    mListView = (ListView) findViewById(R.id.saleList);
    mDrinksAdapter = new drinksAdapter(getApplicationContext(), drink);
    mListView.setAdapter(mDrinksAdapter);

    Button addDrink = (Button) findViewById(R.id.addAnotherDrink);

    addDrink.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SaleActivity.this, drinkSaleList.class);
            intent.removeExtra("DrinkObject");
            intent.putExtra("anotherDrink", true);
            startActivityForResult(intent, 1);

        }
    });

}
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    setIntent(intent);//must store the new intent unless getIntent() will return the old one


}




public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {


            Bundle bn = getIntent().getExtras();

            Drink drink = (Drink) bn.getSerializable("anotherDrinkObject");

            mDrinksAdapter.add(drink);
        }
    }
}

}

A Osman
  • 171
  • 2
  • 8
  • I've never had this scenario before but I understand the part you quoted like "onNewIntent()" has to be in Activity A. Activity B will get the results in "onActivityResult()" as usual. – Bö macht Blau Jan 13 '16 at 07:18

1 Answers1

0

You want to receive the result from Activity B in Activity A so when you are starting Activity B, use startActivityForResult and override onActivityResult in Activity A and you will now receive resultant intent in `onActivityResult

Hope this helps.`

Mustansar Saeed
  • 2,730
  • 2
  • 22
  • 46