本文介绍了真的没有得到的setResult和onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我在这里再次。仍在学习。现在,我需要从2活动传递整数来回。第一个活动传递一个计数器的值第二个(其中跟踪球员的统计资料)。第二活动具有统计重置为零,因此,通过后面的数字能力。但我不能让我的头周围。以下是我迄今为止...

Alright, here I am again. Still learning. Now I need to pass integer values back and forth from 2 activities. First activity passes a counter value to the second (which keeps track of player stats). Second activity has ability to reset stats to zero, hence passing the number back. But I just can't get my head around it. Here's what I have so far...

第一项活动(主):

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.menu_settings:
            Intent i = new Intent(this, Options.class);
            Bundle counters = new Bundle();
            counters.putInt("plWin", plWin);
            counters.putInt("plLoss", plLoss);
            counters.putInt("plDraw", plDraw);
            i.putExtras(counters);
            startActivityForResult(i, ?);
            return true;

请填写?

第二项活动(选项):

public void onBackPressed() {
    super.onBackPressed();
    Intent i = new Intent();
    Bundle counters = new Bundle();
    counters.putInt("Wins", wins);
    counters.putInt("Losses", losses);
    counters.putInt("Draws", draws);
    i.putExtras(counters);
    setResult(?, i);
    finish();
}

再次想不通的?。

和要回我的第一个活动,我不知道发生的事情后:

And going back to my first activity, I don't know what goes after:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

临终摸不着头脑。先谢谢了。

Dying to figure this out. Thanks in advance.

推荐答案

做这样

startActivityForResult(i, 100);  

有关的setResult

For setResult

setResult(RESULT_OK);

您可以使用的setResult更先进的发送结果类似

You can use setresult for more advanced sending of results something like

intent = new Intent();
intent.putExtra("key", "I am done");
setResult(1001, intent);

而在onActivityResult

And in onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {    
   if(requestCode == 100)
   {
    String key = data.getStringExtra("key");
    if(resultcode == 1001 && key!= null && key.equals("I am done"){
       //do something
    }else{
       //do something else
    }

   }
} 

您不必须使用的setResult,如果你需要的是检查你是否从活动回来,然后不要将其设置和不检查onActivityResult

You dont have to use setResult, if all you need to check is whether you returned from the activity then dont set it and dont check in onActivityResult

这篇关于真的没有得到的setResult和onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 11:23