Android的finishActivity总是导致onActivityResult得到RESULT_CANCELED [英] Android finishActivity always causing onActivityResult to get RESULT_CANCELED

查看:1115
本文介绍了Android的finishActivity总是导致onActivityResult得到RESULT_CANCELED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要的活动一个可以揭开序幕子活动B,C或D.我开始他们用startActivityForResult(newIntent,REQUEST_ code);

用户可以选择B之间进行导航,C和D当一个被显示出来。活动A控制的导航。当他们选择屏幕A之间的掉期首先调用finishActivity(REQUEST_ code);上显示的一个,然后调用startActivityForResult(newIntent,REQUEST_ code);为下一个。

在我onActivityResult我有

 保护无效onActivityResult(INT申请code,INT结果code,意图数据)
   {
      //这些日志只是我调试
      Log.w(this.toString(),onActivityResult回用+请求code);
      Log.w(this.toString(),因此code =+结果code);

      //活动设置此值,如果它是返回正常
      如果(RESULT_OK ==结果code)
      {
         Log.i(this.toString(),----子活性恢复行----);
         //做一些东西

      } // end如果(RESULT_OK ==结果code)
      //否则,我们来到这里,因为用户击中背部按钮或出事
      //出了错(RESULT_CANCELED)。无论哪种方式,我们不希望显示此
      //空白屏幕给用户。带他们回设备连接屏幕。
      否则,如果(RESULT_CANCELED ==结果code)
      {
         完();
      }
    }
 

出于某种原因,我得到回零或RESULT_CANCELED每次即使在我的子活动B,C,DI仅当用户选择点击后退按钮设置的结果(我显示你确定对话框,他们选择退出。在B,C,DI拥有的onPause以下的userExiting是一个标志,我从退出对话框进行设置。

 保护无效的onPause()
   {
      Log.i(this.toString(),的onPause);

      //破解尝试的setResult对于其finishActivty被称为一个活动
      如果(!this.exiting)
      {
         Log.i(this.toString(),====== RESULT_OK ======);
         意图returnIntent =新意图();
         的setResult(RESULT_OK,returnIntent);
         完();
      }

      显示= FALSE;

      super.onPause();
   }
 

任何想法?

编辑 - 要明确的问题是,在老年活动呼吁finishActivty,因为结果是从未设置后,一个活动始终认为它应该退出。一个奇怪的侧项目。这种情况不会发生,直到用户导航两次。换句话说B被显示出来,他们选择去C.作品,但我可以在日志中onActivityResult没有被调用中所看到的。用户选择从C去到D,咚,我看到两个呼叫onActivtyResult,'A;出口和D是留给显示,并且用户无法对资产净值的路程。

更新:由于这已经得到了很多的意见,我想我会发布此更新。该项目的发展,使得有时一个活动是显示器,有时一组显示为标签。当用户组,并且该选项卡中需要一些情况下,数据被返回到拉开序幕选项卡中的活动。之前,我在下面加了RESULT_CANCELED code总是被退回。另外,还要确保在的setResult完成之前调用。

 如果(的getParent()== NULL)
 {
    的setResult(Activity.RESULT_OK,意图);
 }
 其他
 {
    的getParent()的setResult(Activity.RESULT_OK,意图)。
 }
 

解决方案

我不太下面的一切,你在这里做,但它听起来并不像你的活动之间导航正常。我会强烈建议您通读记事本的例子彻底让你明白活动之间的移动机器人的方式。

下面是我就明白你想怎么完成的任务:活动A可以导航到B,C或D.从B,C或D,你可以导航到任何其他的

您在某种程度上试图用一个活动作为控制器,让它完成其他活动。不这样做。每个活动应负责在适当的点整理自己。

如果您想从具有去 - 在堆栈上> B到A - > C,那么我会做到这一点:

一个启动B中的结果。当用户选择了一些B中要求去到C,你有几个选择。你可以有B启动C未对结果,然后完成。这将具有从堆栈当C退出被除去B的效果。或者,你可以有乙完成本身,传回一张纸条,C必须在结果意图被加载。然后,可以加载下的结果。

我真的不知道为什么你要这种行为虽然。作为一个Android用户,如果我去从A - > B - > C,我希望打回带我回到B

顺便说一句,你的被退出黑客没有任何意义。到时候你要在onPause的时候,结果已定。您可以在这里不会改变。您需要正确的设置,当你完成,之前的onPause的结果。

I have a main Activity A that can kick off sub-Activities B, C, or D. I start them using startActivityForResult(newIntent, REQUEST_CODE);

The user can choose to navigate between B, C, and D once one is displayed. Activity A controls that navigation. When they choose swap between screens 'A' first calls finishActivity(REQUEST_CODE); on the one displayed and then call startActivityForResult(newIntent, REQUEST_CODE); for the next one.

In my onActivityResult I've got

    protected void onActivityResult(int requestCode, int resultCode, Intent data)
   {
      // these logs are just for my debugging
      Log.w(this.toString(), "onActivityResult returned to by " + requestCode);
      Log.w(this.toString(), "result code = " + resultCode );

      // the Activity set this value if it was returning normally
      if (RESULT_OK == resultCode)
      {
         Log.i(this.toString(), "---- the sub-activity returned OK ----"); 
         // do some stuff    

      }// end if (RESULT_OK == resultCode)
      // Else we got here because the user hit the back button or something went
      // went wrong (RESULT_CANCELED).  Either way we do not want to display this
      // blank screen to the user.  Take them back to the device connection screen.
      else if (RESULT_CANCELED == resultCode)
      {    
         finish();
      }
    }

For some reason I'm getting back a zero or RESULT_CANCELED everytime even though in my sub-activities B,C,D I only set that result if the user chooses to hit the back button (I display an "are you sure" dialog and they choose to exit. In B,C,D I have the following in onPause. The userExiting is a flag I set from the Exit dialog.

protected void onPause()
   {
      Log.i(this.toString(), "onPause");

      // hack to try to setResult for an activity whose finishActivty was called
      if ( !this.exiting ) 
      {
         Log.i(this.toString(), "======== RESULT_OK ========"); 
         Intent returnIntent = new Intent();
         setResult(RESULT_OK, returnIntent);
         finish();
      }

      displayed = false;

      super.onPause();
   }

Any ideas?

Edit - to be clear the issue is that after calling finishActivty on the old Activity, since a result was never set, Activity A always thinks it should exit. One weird side items. This doesn't happen until the user navigates two times. In other words B is displayed and they choose to go to C. Works, but I can see in the log that onActivityResult hasn't been called. User selects to go from C to D, boom, I see two calls to onActivtyResult, 'A; exits and D is left displayed and the user is unable to nav away.

Update: Since this has been getting a lot of views I thought I'd post this update. The project evolved such that sometimes a single Activity is displays and sometimes a group are displayed as tabs. When the user packs out of the tab in some cases data needed to be returned to the Activity that kicked off the tabs. Before I added the code below RESULT_CANCELED would always be returned. Also make sure that setResult is called before finish.

 if (getParent() == null)
 {
    setResult(Activity.RESULT_OK, intent);
 }
 else
 {
    getParent().setResult(Activity.RESULT_OK, intent);
 }

解决方案

I'm not quite following everything you are doing here, but it doesn't sound like you are navigating between activities properly. I would highly suggest reading through the notepad example thoroughly so that you understand the android way of moving between Activities.

Here is what I understand regarding what you are trying to accomplish: Activity A can navigate to B, C or D. From B, C or D you can navigate to any of the others.

You are somehow trying to using Activity A as a controller, having it finish other Activities. Don't do this. Each Activity should be responsible for finishing itself at the proper point.

If you want to go from having A -> B on the stack to A -> C, then I would do this:

A starts B for result. When user selects something in B asking to go to C you have a few choices. You could have B start C not for result, then finish. This would have the effect of B being removed from the stack when C exits. Or, you could have B finish itself, passing back a note that C should be loaded in the results intent. A could then load C for result.

I'm really not sure why you want this behavior though. As an Android user, if I go from A -> B -> C, I expect hitting back to take me back to B.

As an aside, your "is exiting" hack doesn't make any sense. By the time you get to onPause, the result has been set. You cannot change it here. You need to properly set the result when you are finishing, before onPause.

这篇关于Android的finishActivity总是导致onActivityResult得到RESULT_CANCELED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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