Android的onActivityResult未调用

时间:2018-09-03 13:47:25

标签: android android-activity

情况
我在StartActivity的onCreate中开始了2个活动。首先是我的MainActivity,然后是我的UnlockActivity,两者都紧接着。

 private final static int REQUEST_UNLOCK = 13245;
 [...]  
 Intent mainIntent = new Intent(this, MainActivity.class);
 startActivity(mainIntent);
 Intent intent = new Intent(this, UnlockActivity.class);
 startActivityForResult(intent, REQUEST_UNLOCK);

UnlockActivity提示用户输入代码,仅在输入正确的代码后关闭。我的MainActivity包含应用程序的其余部分。

对我来说很重要,活动启动顺序保持不变。 MainActivity处于启动状态时,UnlockActivity确实正在加载。另外,UnlockActivity用于确认整个应用范围内的用户操作,也可以显示在后台计时器上。

这是他们的AndroidManifest条目:

<activity android:name=".main.MainActivity" android:theme="@style/AppTheme" />
<activity android:name=".unlock.UnlockActivity" android:launchMode="singleTop" android:theme="@style/AppTheme" />

问题
无论应用程序是否已解锁一次,我都需要在SharedPrefs中设置一个标志。为此,我尝试使用onActivityResult

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_UNLOCK && resultCode == Activity.RESULT_OK) {
            // i set the flag to true here
        }
    }
}

但是由于某些原因,从未调用onActivityResult。为什么?

1 个答案:

答案 0 :(得分:0)

检查两件事。

  1. 如果需要从MainActivity中的onActivityResult获取数据。然后从MainActivity调用start UnlockActivity
  2. 您是否在UnlockActivity中呼叫了setResult(RESULT_OK, intent)finish();
相关问题