startActivityForResult有三项活动 [英] startActivityForResult with three activities

查看:79
本文介绍了startActivityForResult有三项活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个活动,我们只是叫他们活动A,活动B和活动ç

起初,我在活性的和我开始一个QR扫描仪(活动B)与startActivityForResult是这样的:

 意图我;
I =新的意图(这一点,QrActivity.class);
startActivityForResult(ⅰ,1);
 

在活动B打开,这是code处理结果,并返回到previous活动:

 公共无效handleDe code(结果rawResult,位图吧code)
{

    如果(rawResult!= NULL){
        串textResult = rawResult.getText();

        如果(textResult!= NULL){
            意图returnIntent =新意图();
            returnIntent.putExtra(结果,textResult);
            的setResult(RESULT_OK,returnIntent);
            完();
        }
//的code休息
 

现在这工作完全正常,如果活动A再次打开一切正常,因为它应该。它是这样的:A - > B - > A

这个问题时,我尝试去这样出现:A - >ç - > B - > A

用户有两个选择,要么从开始一个活动或活动C.活动B(QR扫描仪),我总是要处理b活动结果和活动一,即使从活动C. <用户访问b活动/ P>

在我打开从活动C中的活动B(QR扫描仪),这是我试图解决这个问题:

我呼吁活动Ç完成()时,活动B被称为:

 意图;
在=新的意图(这一点,QrActivity.class);
startActivityForResult(以,1);
完();
 

而在活动B,而不是

 意图returnIntent =新意图();
 

我设置的意图是:

 意图returnIntent =新的意图(QrActivity.this,CheckpointsActivity.class);
 

和之后,它可以追溯到活性的什么也没有发生一样,b活动从来没有连开,但似乎并没有得到结果时,从活动下开始b活动。

任何形式的帮助将大大AP preciated!我知道这一切都是非常混乱,但如果你想我可以添加任何更多的细节。

解决方案

这是不如何 startActivityForResult()是为了工作。你会过得更好节省数据共享preferences 或其他地方。

您也可以或许用 startActivityForResult()来启动 ActivityC 从A从此做同样的入门C C(不完成Ç还),传回的结果,你想传递给A.因此,它会是这样

A - >ç - > B(终点) - > C(完成) - > A

使用 startActivityForResult()启动C和B,并返回结果与的setResult() onActivityResult() C和A的

I have three activities, let's just call them Activity A, Activity B and Activity C

At first I'm at the Activity A and I'm starting a QR scanner (Activity B) with startActivityForResult like this:

Intent i;
i = new Intent(this, QrActivity.class);
startActivityForResult(i, 1);

Once the Activity B opens, this is the code for handling result and returning to previous activity:

 public void handleDecode(Result rawResult, Bitmap barcode) 
{

    if (rawResult != null) {
        String textResult = rawResult.getText();

        if (textResult != null) {
            Intent returnIntent = new Intent();
            returnIntent.putExtra("result", textResult);
            setResult(RESULT_OK,returnIntent);     
            finish();       
        }
//rest of the code

Now this works completely fine, once the Activity A is opened again everything works as it should. It goes like this: A --> B --> A

The problem appears when I try to go like this: A --> C --> B --> A

The user has two options, either to start the Activity B (qr scanner) from Activity A or from Activity C. I always want to handle Activity B result with Activity A even if the user accessed Activity B from Activity C.

Once I open the Activity B (qr scanner) from activity C, this is how I tried to solve it:

I called finish() on Activity C when Activity B is called:

Intent in;
in = new Intent(this, QrActivity.class);
startActivityForResult(in, 1);
finish();

And in Activity B, instead of

Intent returnIntent = new Intent();

I'm setting intent as:

Intent returnIntent = new Intent(QrActivity.this, CheckpointsActivity.class);

And after it goes back to activity A nothing happens, like the Activity B was never even opened, it doesn't seem to get the result when Activity B is started from Activity C.

Any kind of help will be greatly appreciated! I know this is all very confusing but I can add any more detail if you want.

解决方案

This is not how startActivityForResult() is meant to work. You would be better off saving the data in SharedPreferences or somewhere else.

You also could probably use startActivityForResult() to start ActivityC from A then do the same to start B from C (don't finish C yet), passing back the result you want to pass to A. So it would be something like

A --> C --> B (finish) --> C (finish) --> A

using startActivityForResult() to start C and B and returning a result with setResult() to the onActivityResult() of both C and A

这篇关于startActivityForResult有三项活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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