如何在重新启动活动后检索意向的新值,而不是原始值



关于这个问题已经有很多问题了,我觉得我的代码应该可以工作了。我正在尝试做的是活动 A 启动活动 B,发送意图。 然后,活动 B 为结果启动活动 A,并且应接收新的意向。我的代码在下面,根据下面文档中的引用,代码应该可以工作:

"

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

因此,基于此,我的代码如下:P.S 我的 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);
        }
    }
}

}

您希望

Activity AActivity B接收结果,因此当您开始Activity B时,在Activity A中使用startActivityForResultoverride onActivityResult,您现在将在'onActivityResult'中收到结果意图

希望这有帮助。

相关内容

  • 没有找到相关文章