安卓入门RESULT_CANCELED时,我专门添加RESULT_OK [英] Android getting RESULT_CANCELED when I specifically add RESULT_OK

查看:1604
本文介绍了安卓入门RESULT_CANCELED时,我专门添加RESULT_OK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题,我有主视图这只能说明一个按钮,pressing该按钮另一种观点表示。该图只有另一个按钮,当该按钮按下这个当前视图共完成和控制背到previous视图。

This is my problem, I have the main view which only shows one button, pressing this button another view is shown. This view has only another button, when this button is push this current view finishs and the control backs to the previous view.

要显示我用startActivityForResult第二个观点,我把code在这里。

To show the second view I use startActivityForResult, I put the code here.

private void startNewview() {       
    Intent it = new Intent(getApplicationContext(), newView.class);
    startActivityForResult(it,VIEW_ID);

}

只有所谓的观点有一个按钮的事件,这里是code

The view called only has a button event, here is the code

Button b = (Button) findViewById(R.id.close);
    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            setResult(RESULT_OK);               
            finish();

        }
    });

最后,在主视图的方法onActivityResult,这里是code

And finally, the method onActivityResult in the main view, here is the code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == VIEW_ID && resultCode == RESULT_OK) {
        tv = (TextView) findViewById(R.id.tv);
        tv.setText("The result ok is  here :)");
    }

}

现在的问题是结果code始终为0 = RESULT_CANCELED,我不知道如何解决它,谁能帮助我?

The problem is resultCode always is 0 = RESULT_CANCELED and I do not know how to solve it, can anyone help me?

非常感谢你!

推荐答案

我无法解释什么是发生在你的code,但我有一个项目样本做到这一点。

I can't explain what is happening in your code but I have a project sample to do this..

只是一个按钮btnFoo一个FooActivity:

a FooActivity with just a button btnFoo:

@Override
protected void onStart() {
    super.onStart();

    btnFoo.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            startActivityForResult(new Intent("xper.activity.ACTIVITY_BAR_INTENT"),1);
        }
    });
}

和一个BarActivity添加在AndroidManifest.xml这样的:

and an BarActivity added in the AndroidManifest.xml like that:

<activity
    android:name = "BarActivity">
    <intent-filter>
        <action
            android:name = "xper.activity.ACTIVITY_BAR_INTENT"/>
        <category
            android:name = "android.intent.category.DEFAULT"/>
    </intent-filter>
</activity>

各code检索意图内的酒吧是在btnBar的onClicEvent(按钮):

The respective code to retrieve the intent inside the bar is in the onClicEvent of the btnBar (Button):

@Override
protected void onStart() {
    super.onStart();

    btnBar.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
            intent.putExtra("codBar", "bar");
            setResult(Activity.RESULT_OK, intent);
            finish();

        }
    });
}

现在,如果你不处理好onActivityResult()事件,当你preSS Android的按钮BACK,你可以得到错误。

Now, if you doesn't handled well the onActivityResult() event, when you press the Android button "BACK", you can get errors.

如果在b活动的意向(意向)是给一些信息的活动时,如果preSS按钮回来,我不知道,如果b活动将在栈中,但有意不这样做。所以,我做了以下内容:

If the Intent (intention) in the Activity B is to give some information to the activity A, if you press the button back, I don't know if the activity B will be in the stack, but the intention isn't done. So I did the following:

@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

    //Intent intent = new Intent("xper.activity.ACTIVITY_BAR_RESULT_INTENT");
    //intent.putExtra("codBar", "bar");
    //setResult(Activity.RESULT_CANCELED, intent);
    setResult(Activity.RESULT_CANCELED);
    finish();
}

处理的信息,我做onActivityResult以下事件(),看看在酒吧活动中获取的信息:

Handling the information I did the following in the event onActivityResult() to see the retrieved information in the Bar Activity:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(data != null) {
        Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode + "\tdata == " + data, 10000).show();
        btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode + "\tdata == " + data /*+ "extras == " + data.getExtras().getString("codBar")*/);
    } else {
        Toast.makeText(this, "BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode, 10000).show();
        btnFoo.setText("BAR\trequestCode == " + requestCode + "\tresultCode  == " + resultCode);
    }

}

如果您有更多的活动,返回信息来源,以父活动好pratices做到以下几点:

if you have more activities to return infomation to the parent activity is good pratices do the following:

//put private static final int globals atributes with the respective name of the
//activity to represent the requestCode for each activity you have like:
private static final int ACTIVITY1 = 117;
private static final int ACTIVITY2 = 118;
...
private static final int ACTIVITYN = 215;

//In the event onActivityResult() is better use the switch statement to handle each
//specific activity to catch information
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == Activity.RESULT_CANCELED) return; // breaks
    //if you decide to handle some information of Activity.RESULT_CANCELED
    //ignore the above condition that returns and handle it inside the switch statement

    switch(requestCode) {
    case ACTIVITY1:
    {
        //Dosomething
    } break;
    case ACTIVITY2:
    {
        //Dosomething
    } break;
    ...
    case ACTIVITYN:
    {
        //Dosomething
    } break;
    }
}

如果你不能做到这一点样品code .. 请给我您的电子邮件,我送FooBarActivity项目

If you can't do this sample code.. please give me your email for me send the FooBarActivity project

这篇关于安卓入门RESULT_CANCELED时,我专门添加RESULT_OK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆