1

关于这个已经有很多问题了,我觉得我的代码应该可以工作。我想要做的是活动 A 启动活动 B,发送一个意图。Activity B 然后为结果启动 Activity A,应该会收到一个新的 Intent。我的代码在下面,根据下面文档中的引用,代码应该可以工作:

“标准”和“singleTop”模式仅在一个方面彼此不同:每次“标准”活动有新的意图时,都会创建一个新的类实例来响应该意图。每个实例 > 处理一个意图。类似地,也可以创建“singleTop”活动的新实例来处理新意图。但是,如果目标任务>已经在其堆栈顶部有一个现有的活动实例,>该实例将接收新意图(在 onNewIntent() 调用中);没有创建新的 >instance。

所以基于此,我的代码如下: PS我的 onNewIntent(Intent) 没有被调用,我想知道我是否把它放在正确的类中。

    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);
            }
        }

    });
}

}

活动 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);
        }
    }
}

}

4

1 回答 1

0

您想从Activity Bin接收结果,Activity A因此当您开始时Activity B,使用startActivityForResultand override onActivityResultin Activity A,您现在将在 `onActivityResult 中收到结果意图

希望这会有所帮助。`

于 2016-01-13T07:20:55.373 回答